Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What does userdata means?And what is the case we can use it? #513

Closed
dota17 opened this issue Dec 17, 2019 · 3 comments
Closed

What does userdata means?And what is the case we can use it? #513

dota17 opened this issue Dec 17, 2019 · 3 comments

Comments

@dota17
Copy link
Member

dota17 commented Dec 17, 2019

I don't understand the meaning of the userdata.
It seems that it will cover the value jso->o.** and the method json_object_to_json_string but the values are not same.

@ploxiln
Copy link
Contributor

ploxiln commented Dec 18, 2019

The userdata can just be NULL if you want. It's just for you to use it for whatever you need, in order to distinguish between different kinds of json objects in a common delete callback function, or in order to stash a special representation for some objects. It's just for your convenience. If you don't need it, just use NULL.

@dota17
Copy link
Member Author

dota17 commented Dec 18, 2019

I find these case:

  1. I can use json_object_set_serializer to set my userdata and to_string_func which will cover the old value json->o.**
  2. I can set different format to json_object by using userdata when it prints a double value
  3. I can put any data to userdata by json_object_set_userdata but do nothing, it just store the value.

Are these all right?

@hawicz
Copy link
Member

hawicz commented Dec 18, 2019

1 & 2 are essentially the same thing, it just that for # 2 json-c happens to provide a json_object_double_to_json_string() serialization function that uses userdata as a printf format if it is set. (you still need to call json_object_set_serializer)
1b. When json double objects are parsed, the code in json_tokener_parse_ex() will use json_object_new_double_s() which calls json_object_set_serializer() with yet another built-in serializer, json_object_userdata_to_json_string(), to ensure the output is identical to the original input.
Note, however, that these only "cover" the value when serializing, and don't affect what you get back from e.g. json_object_get_double().

  1. The userdata field is "arbitrary user-provided data", that you can use for whatever you want.
    You can get the data back out with json_object_get_userdata(), so that's not useless, you can come up with your own code to use that data while walking an object tree or otherwise processing a json_object in some way. The serializer functions use of userdata is just a particular instance of that which happens to be triggered when json_object_to_json_string() is called. They stretch the meaning of "user-provided" a bit, but it seemed better to do that than to increase memory usage by adding even more mostly-unused pointers to the json_object structure.

@hawicz hawicz closed this as completed Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants