开发者

json serialization of floating-point values

开发者 https://www.devze.com 2023-02-21 01:27 出处:网络
I\'m writing a JSON library for C (see https://github.com/DanielWaterworth/Butterfly ). I can serialize every other data type, but floating-points have stumped me. I need to write 2 functions:

I'm writing a JSON library for C (see https://github.com/DanielWaterworth/Butterfly ). I can serialize every other data type, but floating-points have stumped me. I need to write 2 functions:

  • The first takes as an argument the number开发者_如何学JAVA (as a double) to be serialized and returns the length that the serialized string would be (minus the null terminator).
  • The second takes as arguments the number (as a double) and a buffer (which is at least as long as the output of the previous function). It then writes to the buffer and returns the length of the serialized string.

All I need is some helpful hints to get me started.


I have a working implementation using snprintf and asprintf. It seems that this is the technique that python's repr function used to use until they moved to a dtoa based approach. I'm still interested in other approaches.

0

精彩评论

暂无评论...
验证码 换一张
取 消