LitJSON / litjson

JSON library for the .Net framework
https://litjson.net/
Other
1.37k stars 404 forks source link

Fix empty lists and objects by forcing JsonData to assign a type. #29

Closed uncle-betty closed 8 years ago

uncle-betty commented 10 years ago

Hey there,

LitJSON outputs invalid JSON in an edge case. I sometimes read JSON from a file into a JsonData object and write it back without touching it. That breaks empty lists and objects in the output.

When reading an empty list or an empty object, the type of a JsonData object remains at JsonType.None. In contrast, for non-empty lists, for example, the parser would invoke IList.Add() at least once, which would make JsonData set the correct type via EnsureList().

I fixed it - possibly a little naively - by invoking IList.Clear() and IDictionary.Clear() for instantiated lists and objects, respectively, which is a no-op with the side effect of making the JsonData object set its type.

I have demo code to reproduce the issue. (And I had hoped that I could attach files to a pull request...)

Thomas