Hi,
I test TMS Sparkle for an integration in a existing projet.
It sounds great.
But if i want serialize a Object in a Object with a condition. See this exemple.
TPerson = class
Name : String;
Age : Integer;
Brother : TPerson;
End;
But Brother is not every time assigned so i must put a condition in the serialize function like that maybe :
Json
.WriteBeginObject
.WriteName('name')
.WriteString(Person.name)
.WriteName('age')
.WriteInteger(person.age);
//Here the condition
if assigned(Person.Brother) then begin
Json
.WriteBeginObject
.WriteName('brothername')
.WriteString(Person.brother.name)
.WriteName('brotherage')
.WriteInteger(person.brother.age)
WriteEndObject;
end;
Json
.WriteEndObject;
But this exemple doesn't work (Error : Invalid nesting,Not all objects were properly closed)
Is ther an another method? Thank's for your help.
Nicolas.
wlandgraf
(Wagner Landgraf)
September 19, 2014, 10:58am
2
Hi, you forgot the name of property. You are trying to add Brother object directly inside the outer object without giving a name for the property (name/value pair). Either use this:
Or this:
no one of the two solution works, sorry. And raise the same error.
there is my full code, you see another mistake maybe?
Json := TJsonWriter.Create(Stream);
Json.IndentLength := 5;
Json
.WriteBeginObject
.WriteName('stationname')
.WriteString(CTimeManager.WorkShops.WorkShops.Stations[j].Name)
.WriteName('workshopname')
.WriteString(CTimeManager.WorkShops.WorkShops.Name)
.WriteName('state')
.WriteString(CurrentState.Name)
.WriteName('isproductive')
.WriteBoolean(CurrentState.Productive)
.WriteName('color')
.WriteInteger(ColorToRGB(CurrentState.Color));
if Assigned(CurrentState.Parent) then begin
Json
.WriteName('parentstate')
.WriteBeginObject
.WriteString(CurrentState.Parent.Name)
.WriteName('parentisproductive')
.WriteBoolean(CurrentState.Parent.Productive)
.WriteName('parentcolor')
.WriteInteger(ColorToRGB(CurrentState.Parent.Color))
.WriteEndObject;
end;
Json
.WriteEndObject;
Json.Free;
wlandgraf
(Wagner Landgraf)
September 19, 2014, 11:23am
4
Again, you are writing a string without giving it a name:
.WriteBeginObject
// YOU MUST WRITE NAME HERE:
// .WriteName(SomeName)
.WriteString(CurrentState.Parent.Name)