You are not logged in.
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
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?
Online
Ok... I'll prepare an example.
Offline
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
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
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
Online
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!
Online
Thank you, the fix works.
I will also have a look at TSynPersistent and TSynAutoCreateFields as you suggested.
Offline