[FS] JSON Writer

George_Anderson
George_Anderson Member Posts: 71 ✭✭
Has anyone written a function to convert a map to a JSON string? (Basically the inverse of parseJsonWithUnits).

Just looking for something rudimentary. It may be hard to do this if the map contains non-basic types. (e.g. after serializing a "plane", it wouldn't be straightforward to know that you needed to interpret the map as a plane when deserializing).

Comments

  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,278 image
    See the toString function implementations in string.fs -- that doesn't exactly produce JSON, but it's close in a number of cases.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Ethan_K
    Ethan_K Member, Onshape Employees Posts: 57 image
    edited January 2021
    I made this. It isn't a complete implementation (funkiness with arrays) but it does quite well. Hope that helps! Also, if you come up with improvements, I would happily integrate them. The feature is just so I can easily test it - to use in FS, you should just use `mapToJSON` feature.
  • George_Anderson
    George_Anderson Member Posts: 71 ✭✭
    edited January 2021
    @ilya_baran Excellent, this is generally working nicely.
    I have what appears to be a bug with the JSON unit parser, but I may be mis-using something:
    println(parseJsonWithUnits("[\"-1.0 meter\"]"));
    Prints "[1.0 meter]"
    (It makes negative values with units positive). It doesn't happen with unitless negative values.

  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,278 image
    Good catch -- thanks.  I can see where it is -- logging it.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • George_Anderson
    George_Anderson Member Posts: 71 ✭✭
    For any future reader here, the bug has been fixed.
  • George_Anderson
    George_Anderson Member Posts: 71 ✭✭
    edited March 2021
    I eventually made this mini-library:
    It's very much inspired by the OnShape stdlib string functions, and it also includes a template for how you can restore types after deserializing.