Jump to content
chkaufmann

Problem with JSON library "SuperObjects"

Recommended Posts

I'm using Superobjects to handle JSON data. Now I have to handle a result, where there are identifiers with dots:

 

{ "splash.annually": "normal" }

 

This library cannot handle this data because it looks at "splash.annually" as path.

 

Can somebody recommend me another library? What I like with Superobjects: It is fast and it offers reference counted object handling using interfaces.

 

Regards
Christian

Share this post


Link to post

I do not know about Superobjects but Delphi standard json library can do following

uses
  System.JSON,
  System.Generics.Collections;

procedure TForm1.Button1Click(Sender: TObject);
const
  JsonText = '{ "splash.annually": "normal" }';
var
  Json: TJSONValue;
begin
  Json := TJSONObject.ParseJSONValue(JsonText, False, False);
  if Json <> nil then
  begin
    if Json.FindValue('["splash.annually"]') <> nil then
    begin
      ShowMessage(Json.FindValue('["splash.annually"]').AsType<string>);
    end;
    Json.Free();
  end;
end;

 

Share this post


Link to post
18 hours ago, chkaufmann said:

Can somebody recommend me another library?

In mORMot there are many ways to process JSON. mORMot is at least factor 5 faster than your current solution and more than 20 times faster than Delphi's own classes. A few of several possibilities are:

var
  v: Variant;
begin
  v := _JsonFast('{"splash.annually": "normal"}');
  ShowMessage(v.Value('splash.annually'));

var
  doc: TDocVariantData;
begin
  doc.InitJson('{"splash.annually": "normal"}');
  ShowMessage(doc.S['splash.annually']);

I have published an article on this topic here (forum Delphi-PRAXIS). Here is the translation into English with Google Translator. The result is not perfect (rather not so good), also some formatting is destroyed, but it is readable.

 

With best regards
Thomas

Share this post


Link to post
1 hour ago, Bob Devine said:

It was the best library when I tested a couple years back for handling nested objects...

What problems did you have with nested objects? mORMot can serialize almost anything to JSON. And if you use class TSynAutoCreateFields, you don't have to create or destroy the child object(s) yourself. It is enough to write the following:

type
  {$M+}
  TSubItem = class(TObject)
  private
    FSubValue: RawUtf8;
  published
    property SubValue: RawUtf8
      read FSubValue write FSubValue;
  end;
  {$M-}

  TItem = class(TSynAutoCreateFields)
  private
    FValue: RawUtf8;
    FSubItem: TSubItem;
  published
    property Value: RawUtf8
      read FValue write FValue;
    property SubItem: TSubItem
      read FSubItem;
  end;

var
  item: TItem;
  list: TObjectList;
begin
  list := TObjectList.Create;
  try
    for var i: Integer := 0 to 1000 do
    begin
      item := TItem.Create;
      item.Value := StringToUtf8('value' + i.ToString);
      item.SubItem.SubValue := StringToUtf8('subValue' + i.ToString);
      list.Add(item);
    end;

    ObjectToJsonFile(list, '_listData.json');
  finally
    list.Free;
  end;

With best regards
Thomas

Edited by mytbo

Share this post


Link to post
On 8/5/2023 at 10:48 PM, chkaufmann said:

This library cannot handle this data because it looks at "splash.annually" as path

Probably there is some option to not use paths or redefine path delimiters?

Share this post


Link to post
12 hours ago, mytbo said:

What problems did you have with nested objects?

To be honest I can't remember. It was about 4 years ago and no doubt mORMot will now have it sorted out 🙂 I use JSON for config files on the desktop and at the time Neon was the only one that coped with the complex configs I was using. I use mORMot JSON for everything else where performance is important (I never tested Neon for performance).

 

Cheers, Bob

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×