Skip to content

Conversation

MarkaPola
Copy link
Contributor

...f templates

This change is a proposition to extend basic_json class, as well as support ones (const_val_proxy and object_key_proxy), with two template methods enabling dynamic type handling. This extension is useful in case of usage of json as part of some other template.

To check current type of json object:
template
bool basic_json::is ();

types basic_json::object_type and basic_json::array_type are added to enable usage of method is<> with specific json objects.

To retrieve types value from json object:
template
T basic_json::as ();

danielaparker added a commit that referenced this pull request Jan 22, 2014
add template methods is<> and as<> to facilitate json handling as part o...
@danielaparker danielaparker merged commit 8612f5c into danielaparker:master Jan 22, 2014
@danielaparker
Copy link
Owner

Hi Marc,

I merged the changes into master. I also added some test cases in json_accessor_tests.cpp.

I have no issue with as<>. I added support for as and as.

is<> I'm less sure of. As implemented, we have

    json obj;
    obj["field1"] = 10;
    obj["field1"].type() is json::longlong_t
    // In code, 10 becomes a  longlong_t and 10U becomes a  ulonglong_t

    obj["field1"].is() is true
    obj["field1"].is() is true
    obj["field1"].is() is false
    obj["field1"].is() is false
    obj["field1"].is() is false

On the other hand

    json obj = json::parse_string("{"field1":10}");  

    obj["field1"].type() is json::ulonglong_t
    // When parsing, all numbers without minus signs become ulonglong_t

    obj["field1"].is() is false
    obj["field1"].is() is false
    obj["field1"].is() is true
    obj["field1"].is() is true
    obj["field1"].is() is false

In this implementation, is means neither that the value has type T internal representation, nor that the internal representation can be coerced into type T (such as with as<>.) I think it has to mean one or the other, and I think the second meaning would be more useful to the programmer. If we accept the second meaning, then I would expect to see in both cases above

    obj["field1"].is() is true
    obj["field1"].is() is true
    obj["field1"].is() is true
    obj["field1"].is() is true
    obj["field1"].is() is true

I think is, is etc should check bounds and return false if the internal value exceeds the bounds. I also think is, isetc should check the sign if the internal rep is signed and return false if negative.

Does this sound reasonable?

Thanks,
Daniel

On Tuesday, January 21, 2014 5:56:56 AM, Marc Chevrier [email protected] wrote:

...f templates
This change is a proposition to extend basic_json class, as well as support ones (const_val_proxy and object_key_proxy), with two template methods enabling dynamic type handling. This extension is useful in case of usage of json as part of some other template.
To check current type of json object:
template
bool basic_json::is ();
types basic_json::object_type and basic_json::array_type are added to enable usage of method is<> with specific json objects.
To retrieve types value from json object:
template
T basic_json::as ();


You can merge this Pull Request by running
git pull https://github.com/MarkaPola/jsoncons feature/types-management
Or view, comment on, or merge it at:
  #3
Commit Summary
* add template methods is<> and as<> to facilitate json handling as part of templates
File Changes
* M src/jsoncons/json1.hpp (217)
* M test_suite/src/json_accessor_tests.cpp (15)
* M test_suite/src/json_array_tests.cpp (19)
* M test_suite/src/json_object_tests.cpp (12)
* M test_suite/src/jsoncons_test.cpp (16)
Patch Links:
* https://github.com/danielaparker/jsoncons/pull/3.patch
* https://github.com/danielaparker/jsoncons/pull/3.diff

Reply to this email directly or view it on GitHub.

danielaparker added a commit that referenced this pull request Jan 13, 2016
add template methods is<> and as<> to facilitate json handling as part o...
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

Successfully merging this pull request may close these issues.

2 participants