Simple sorting objects

Json support is one of the new features of delphi 2009 and delphi 2010. I want to know if there is any simple function for sorting / disassembling directly between a string and an object, for example, in a superobject library.

Example:

MyKnownObject := FromJSON('{name:"francis", surname:"lee"}'); 
+4
source share
2 answers

Non-serialize string directly in TJSONObject

 var ConvertFrom: String; JSON: TJSONObject; StringBytes: TBytes; I: Integer; begin ConvertFrom := '{"name":"somebody on SO","age":"123"}'; StringBytes := TEncoding.ASCII.GetBytes(ConvertFrom); JSON := TJSONObject.Create; try JSON.Parse(StringBytes, 0); Assert(JSON.ToString = ConvertFrom, 'Conversion test'); Memo1.Lines.Add(JSON.ToString); for I := 0 to JSON.Size - 1 do Memo1.Lines.Add(JSON.Get(I).JsonString.Value + ' : ' + JSON.Get(I).JsonValue.Value); finally JSON.Free; end; end; 
+1
source

See here . Bellow is great news:

 procedure TForm13.Button4Click(Sender: TObject); var LContact: TContact; oMarshaller: TJSONMarshall; crtVal: TJSONValue; begin LContact:=TContact.Create; //our custom class LContact.Name:='wings-of-wind.com'; LContact.Age:=20; //fill with some data oMarshaller:=TJSONMarshal.Create(TJSONConverter.Create); //our engine try crtVal:=oMarshaller.Marshal(LContact); //serialize to JSON Memo1.Text:=crtVal.ToString; //display finally //cleanup FreeAndNil(LContact); FreeAndNil(oMarshaller); end; end; 

You can also see here a more complex example of Adrian Andrei (DataSnap architect), as well as an example of custom marshaling here .

+2
source

All Articles