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

Add array notation to dotget/dotset functions #57

Open
kgabis opened this issue Sep 25, 2016 · 5 comments
Open

Add array notation to dotget/dotset functions #57

kgabis opened this issue Sep 25, 2016 · 5 comments
Assignees

Comments

@kgabis
Copy link
Owner

kgabis commented Sep 25, 2016

Example:

/* Initialising object with an array of strings */
JSON_Value *val = json_value_init_object();
JSON_Object *obj = json_object(val);
json_object_dotset_string(obj, "array[0]", "item0");
json_object_dotset_string(obj, "array[1]", "item1");

json_object_dotset_string(obj, "array[3]", "item3"); /* error, index 2 is missing */

json_object_dotset_string(obj, "array[2]", "item2");
json_object_dotset_string(obj, "array[3]", "item3"); /* ok */

/* Result:
{ "array": ["item0", "item1", "item2", item3"] }
*/

/* Getting a string at index */
const char *str = json_object_dotget_string(obj, "array[0]");
@kgabis kgabis self-assigned this Sep 25, 2016
@mu578
Copy link

mu578 commented Sep 9, 2017

something like an_array.[1].an_object.key would be easier.

is_key_index(const char * path_elem) { ... }

@woodruffw
Copy link

As a consumer of this library, some kind of index-independent syntax would also be nice:

json_object_dotset_string(obj, "array[]", "itemN");

Where "array[]" indicates that the value should be appended to the array.

@Ethan-J-Liang
Copy link

Array notation is also something I was looking for. Like moe123's comment, some straightforward addressing scheme such as an_array.[1].an_object.key to access a certain element in an array will be very good.

@lidiahhh
Copy link

Hi,
I tried your code:

JSON_Value *cloud_pkt_value = json_value_init_object();
JSON_Object *cloud_pkt_object = json_object(cloud_pkt_value);

json_object_dotset_string(cloud_pkt_object, "array[0]", "item0");
json_object_dotset_string(cloud_pkt_object, "array[1]", "item1");

and then added
char * serialized_string = json_serialize_to_string(cloud_pkt_value );

but when i print serialized_string, i get:
{"array[0]":"item0","array[1]":"item1"}
instead of { "array": ["item0", "item1"]}.

Can you explain better how to create arrays?
thanks

@kgabis
Copy link
Owner Author

kgabis commented Mar 31, 2021

@lidiahhh it's not implemented, it's only a suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants