Show: Delphi C++
Display Preferences

JSON

From RAD Studio
Jump to: navigation, search

Go Up to Using the RTL in Multi-Device Applications


JSON (JavaScript Object Notation) is a language independent lightweight data-interchange format. JSON can be used as an alternative to other data-interchange formats such as XML or YAML.

RAD Studio provides JSON frameworks that contain classes and methods to store, parse, read, write, and generate data in JSON format.

JSON Frameworks

RAD Studio provides different frameworks to handle JSON data.

JSON Objects Framework

The JSON objects framework requires you to create a temporary object to parse or generate JSON data. To read or write JSON data, you have to create an intermediate memory object such as TJSONObject, TJSONArray, or TJSONString before reading and writing the JSON.

For further details about this framework, see JSON Objects Framework.

Readers and Writers JSON Framework

The readers and writers JSON framework allows you to read and write JSON data directly to a stream, without creating a temporary object. Since you do not have to create temporary object to read and write a JSON, this framework has a better performance, and improves memory consumption.

Unlike the JSON objects framework; the readers and writers JSON framework provides BSON support.

For further details about this framework, see Readers and Writers JSON Framework.

JSON Framework Differences

You can use any of the two frameworks when working with JSON data. You may choose one or the other according to the needs of the project. The below table highlights some key differences among both JSON frameworks.

JSON Objects Framework
  • Easier way to read a JSON structure, allows iterations and modifications in the model tree.
Readers and Writers JSON Framework
  • Reads and writes JSON in a stream in a sequential manner, which reduces memory consumption.
  • BSON support.
  • Extensible.

The below table has three code snippets that illustrate the differences between the frameworks when writing a JSON.

Writing a JSON With Different Frameworks
JSON Objects Framework Readers and Writers JSON Framework: TJSonWriter Readers and Writers JSON Framework: TJSONObjectBuilder
JSONColor := TJSONObject.Create;
JSONColor.AddPair('name', 'red');
JSONColor.AddPair('hex', '#f00');
JSONArray := TJSONArray.Create;
JSONArray.Add(JSONColor);
JSONObject := TJSONObject.Create;
JSONObject.AddPair('colors', JSONArray);
Writer.WriteStartObject;
Writer.WritePropertyName('colors');
Writer.WriteStartArray;
Writer.WriteStartObject;
Writer.WritePropertyName('name');
Writer.WriteValue('red');
Writer.WritePropertyName('hex');
Writer.WriteValue('#f00');
Writer.WriteEndObject;
Writer.WriteEndArray;
Writer.WriteEndObject;
Builder := TJSONObjectBuilder.Create(Writer);
  Builder
    .BeginObject
      .BeginArray('colors')
        .BeginObject
          .Add('name', 'red')
          .Add('hex', '#f00')
        .EndObject
    .EndArray
  .EndObject;

The three codes snippets of the above table write the following JSON:

{
   "colors":[
      {
         "name":"red",
         "hex":"#f00"
      }
   ]
}


The below table has three code snippets that illustrate the differences between the frameworks when reading a JSON.

Reading a JSON With Different Frameworks
JSON Objects Framework Readers and Writers JSON Framework: TJSonReader Readers and Writers JSON Framework: TJSONIterator
JSONValue := TJSONObject.ParseJSONValue('{"colors":[{"name":"red", "hex":"#f00"}]}');
 
Memo1.Lines.Add('READER:');
if JSONValue is TJSONArray then
  //... 
else if JSONVAlue is TJSONObject then
  Memo1.Lines.Add('colors');
  Memo1.Lines.Add('name: '+ JSONValue.GetValue<string>('colors[0].name'));
  Memo1.Lines.Add('hex: '+ JSONValue.GetValue<string>('colors[0].hex'));
LStringReader := TStringReader.Create('{"colors":[{"name":"red", "hex":"#f00"}]}');
LJsonTextReader := TJsonTextReader.Create(LStringReader);
 
while LJsonTextReader.read do
  case LJsonTextReader.TokenType of
    TJsonToken.PropertyName:
    Memo1.Lines.Add(LJsonTextReader.Value.AsString);
	TJsonToken.String:
    Memo1.Lines[Memo1.Lines.Count-1] := Memo1.Lines[Memo1.Lines.Count-1] + ': ' +LJsonTextReader.Value.AsString;
  end;
LStringReader := TStringReader.Create('{"colors":[{"name":"red", "hex":"#f00"}]}');
LJsonTextReader := TJsonTextReader.Create(LStringReader);
LIterator := TJSONIterator.Create(LJsonTextReader);
 
LIterator.Recurse;
LIterator.Next;
Memo1.Lines.Add(LIterator.Key);
LIterator.Recurse;
LIterator.Recurse;
LIterator.Next;
LIterator.Recurse;
LIterator.Next;
Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString);
LIterator.Next;
Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString);

The three codes snippets of the above table add the following to a TMemo.

colors
name: red
hex: #f00

JSON Topics

  • JSON Frameworks:

See Also

Code Samples

Personal tools
In other languages
Previous Versions
Assistance