ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.12k stars 267 forks source link

Ability to output empty objects as null instead of {} #179

Open OSalama opened 2 months ago

OSalama commented 2 months ago

Is your feature request related to a problem? Please describe. I'm loading data output by yyjson into Spark/Hive tables, and the fact that empty objects are output as {} instead of null is causing a lot of issues, because Spark is not able to infer any meaningful schema and ends up dropping the empty object fields.

Describe the solution you'd like Output empty objects as null instead of {}. I'd be happy to have to use a flag to achieve this.

Describe alternatives you've considered The only alternatives I can see would be outside the scope of yyjson, eg:

  1. post-processing to convert all {} to null - this feels like it would be really slow and defeat a lot of the point of using yyjson.
  2. upstream code changes to ensure we define a schema for our empty dictionaries, so {} becomes {optional_field_1: null, optional_field_2:null, ...} - this feels "wrong", since there is a distinction between a null object and an object with keys and null values.
  3. define a full schema for our Spark tables. This will work, but it's not how I'd like to do things on this particular project for other reasons.

Additional context N/A

ibireme commented 2 months ago

It seems this requirement might not be very common, could you provide more details about how you're using yyjson to output JSON?

If you're building JSON using yyjson_mut_val, I suggest adding a single line of code after you finish creating an object:

yyjson_mut_val *obj = ...
if (!yyjson_mut_obj_size(obj)) yyjson_mut_set_null(obj);