Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

[FS] JSON Writer

George_AndersonGeorge_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

  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,178
    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
  • Options
    Ethan_KEthan_K Member, Onshape Employees Posts: 57
    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.
  • Options
    George_AndersonGeorge_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.

  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,178
    Good catch -- thanks.  I can see where it is -- logging it.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Options
    George_AndersonGeorge_Anderson Member Posts: 71 ✭✭
    For any future reader here, the bug has been fixed.
  • Options
    George_AndersonGeorge_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.
Sign In or Register to comment.