#1 2015-03-14 23:27:36

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

Please consider the following line of code:

JSONToObject(Self, @json[1], Result, nil, [j2oIgnoreUnknownProperty]);

If I ADD a property to my object, and such property is NOT in my JSON string, it works as expected, it just ignores the property that wasn't found in the JSON string.

But if I REMOVE a property from my object, and such field still exists in my JSON string... then my object will be deserialized only in part. All properties of my object that follow the property missing from the JSON string will not be set to the values found in the JSON string.

I would expect j2oIgnoreUnknownProperty to work in a bi-directional way: whatever is missing on either side (object or JSON) is ignored, but the process continues and every other field after the missing one will still be read from the JSON and set into the object.

What am I missing?

Offline

#2 2015-03-15 18:45:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

It should not be the case...
There is the following code in JsonToObject():

    P := ClassFieldPropWithParentsFromUTF8(ValueClass,PropName);
    if P=nil then // unknown property
      if j2oIgnoreUnknownProperty in Options then begin
        From := GotoNextJSONItem(From,1,@EndOfObject);
        continue;
      end else
        exit; // by default, abort

So a JSON field missing in the object should not block the content.
Could you try to find out why it is not the case?

Offline

#3 2015-03-15 19:06:16

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

Ok... I'll prepare an example.

Offline

#4 2015-03-15 19:32:44

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

Ok, I've prepared a test project to show you the behavior. It would require too much posting of code here... if you see the example I prepared you'll understand in 2 seconds. How do I send you the app? There's no "attach file" feature in this forum. Is it ok if I send it to you via email?
If so, please, tell me which email address should I use, as the email feature of this forum also doesn't allow sending attachments.
Thank you.

Last edited by BBackSoon (2015-03-15 19:34:11)

Offline

#5 2015-03-16 05:14:49

zed
Member
From: Belarus
Registered: 2015-02-26
Posts: 105

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

BBackSoon
Try this: https://gist.github.com/

Gist is a simple way to share snippets and pastes with others. All gists are Git repositories, so they are automatically versioned, forkable and usable from Git.

Offline

#6 2015-03-16 07:49:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

I checked the source.

Some remarks:
1. I did not have the UPermissions.pas unit - so I created a blank one.
2. Consider using TSynPersistent and TSynAutoCreateFields instead of TPersistent + custom Create/Destroy: those classes are easier to work with, when nesting complex objects.
3. Also missing unit IOUtils.pas - I used SynCommons.pas' StringFromFile() instead of TFile.ReadAllText.
4. In our DDD domain code, we define TSynPersistent and TSynAutoCreateFields classes, and use T*ObjArray instead of TCollections or TList.
5. Your particular issue should be fixed by http://synopse.info/fossil/info/79148a2918

Offline

#7 2015-03-16 08:18:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

zed wrote:

BBackSoon
Try this: https://gist.github.com/

I've added gist to the "Rules" list of this forum.
See http://synopse.info/forum/misc.php?action=rules

Thanks for the idea!

Offline

#8 2015-03-16 17:50:20

BBackSoon
Member
Registered: 2014-11-15
Posts: 41

Re: JSONtoObject and j2oIgnoreUnknownProperty: odd behavior

Thank you, the fix works.
I will also have a look at TSynPersistent and TSynAutoCreateFields as you suggested.

Offline

Board footer

Powered by FluxBB