Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2010-09

JSON Beautifier

11 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

theo

Hello,

Has anybody written some pascal code to make the output of "TJSONData.AsJSON" more readable for the human eye? It is a one-liner, I'd like to have something like this (example formatted with NetBeans IDE):

[{
"id":1,
"s_name":"startscreen",
"i_duration":5000,
"i_dayofweek":0,
"i_timetype":0,
"frames":[{
"id":1,
"layoutid":1,
"s_name":"top",
"i_top":10,
"i_left":200,
"i_width":200,
"i_height":200,
"texts":[{
"s_url":"http:\/\/www.test.ch\/lazarus\/",
"i_contenttype":1,
"s_html":"",
"s_name":"left",
"i_duration":5000,
"i_timetype":0,
"i_dayofweek":0
}]
etc.

Thank you
Theo

Reply by email

Michael Van Canneyt
to theo
theo wrote
Hello, Has anybody written some pascal code to make the output of "TJSONData.AsJSON" more readable for the human eye?

Have you tried the JSONVIewer app in Lazarus ? tools/jsonviewer ? It displays (and allows to manipulate) the JSON in a completely graphical manner.

I'm already working on a formatted JSON call, but I'm not yet there.

Michael.

Reply by email

theo
Have you tried the JSONVIewer app in Lazarus ? tools/jsonviewer ? It displays (and allows to manipulate) the JSON in a completely graphical manner.

Now I had the time. Looks great! Thanks for this.
(I had to copy the json files from FPC trunk, it didn't compile with my 2.4 because of "Extract")

I'm already working on a formatted JSON call, but I'm not yet there.

This would be great for debugging using ShowMessage or writeln. Any idea when this feature will be ready?

I have another question.
What exactly is the purpose of TJSONNull in fpjson?

In Javascript, i can do sth. like:

var myJSONObject =
{
  "Name": "test",
  "testDate": null
};
alert(myJSONObject.testDate);
myJSONObject.testDate=Date();
alert(myJSONObject.testDate);

Which I understand as: "There is no date assigned initially, but this can be changed". I can't figure out how to do this with fpjson. except probably deleting and adding the value/type.

Thank you
Theo

Reply by email

Michael Van Canneyt
to theo
theo wrote 12 lines
> Have you tried the JSONVIewer app in Lazarus ? tools/jsonviewer ? > It displays (and allows to manipulate) the JSON in a completely graphical > manner. Now I had the time. Looks great! Thanks for this. (I had to copy the json files from FPC trunk, it didn't compile with my 2.4 because of "Extract") > I'm already working on a formatted JSON call, but I'm not yet there. This would be great for debugging using ShowMessage or writeln. Any idea when this feature will be ready?

Somewhere next week.

I have another question. What exactly is the purpose of TJSONNull in fpjson?

To represent a null value ?

Show quoted text 14 lines
In Javascript, i can do sth. like: var myJSONObject = { "Name": "test", "testDate": null }; alert(myJSONObject.testDate); myJSONObject.testDate=Date(); alert(myJSONObject.testDate); Which I understand as: "There is no date assigned initially, but this can be changed". I can't figure out how to do this with fpjson. except probably deleting and adding the value/type.

You can create the above object with:

myJSONObject:=TJSONObject.Create(['name', 'test', 'testdate', Nil]);

and then

myjson.types['testdate']=jtNull

or

myjson.isnull['testdate'] = true

will evaluate to true.

After that you can do

myJSONObject['testdate']:=TJSONDate.Create('2010/11/10');

Note that JSON does not know the date type.

Michael.

Reply by email

theo
>/ Any idea when this feature will be ready?

/> Somewhere next week.

Great.

myJSONObject['testdate']:=TJSONDate.Create('2010/11/10');

I see. Thank you

Regards
Theo

Reply by email

Michael Van Canneyt
to theo

Hello,

Just a short follow-up, to say that I've committed support (a bit later than planned) for FormatJSON() which has several options. Revision 16146.

Function FormatJSON(Options : TFormatOptions = DefaultFormat; Indentsize : Integer = DefaultIndentSize) : TJSONStringType;

Options:

   TFormatOption = (foSingleLineArray,   // Array without CR/LF : all on one line
                    foSingleLineObject,  // Object without CR/LF : all on one line
                    foDoNotQuoteMembers, // Do not quote object member names.
                    foUseTabchar);       // Use tab characters instead of spaces.
   TFormatOptions = set of TFormatOption;

And defaults:

Const
   DefaultIndentSize = 2;
   DefaultFormat     = [];
   AsJSONFormat      = [foSingleLineArray,foSingleLineObject]; // These options make FormatJSON behave as AsJSON

If need be, more options can be added. But the above seem like the logical ones.

Michael.

theo wrote 20 lines
>> / Any idea when this feature will be ready? /> Somewhere next week. Great. > myJSONObject['testdate']:=TJSONDate.Create('2010/11/10'); I see. Thank you Regards Theo _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

theo

Thank you Michael, great!

There is one little problem: the right square bracket which is closing an array always goes to the first position (no indentation). And it's a matter of taste, if brackets and parenthesis should go to the same line.
I've attached an example. The FormatJSON output (above) and the NetBeans Formatter output (below).

Thanks
Theo

-------------------------------------------
{
"i_width" : 1280,
"i_height" : 1024,
"layouts" : [
{
"s_name" : "Unbenannt_1",
"i_duration" : 3000,
"t_starttime" : "null",
"t_endtime" : "null",
"t_startdate" : "null",
"t_enddate" : "null",
"i_dayofweek" : 0,
"i_timetype" : 0,
"frames" : [
{
"s_name" : "Unbenannt",
"i_top" : 10,
"i_left" : 200,
"i_width" : 200,
"i_height" : 200,
"texts" : [
{
"s_url" : "test.php",
"i_contenttype" : 1,
"s_html" : "{0D1FB698-709D-4817-9F76-34E8960CE4F2}", "s_name" : "Unbenannt",
"i_duration" : 5000,
"t_starttime" : "null",
"t_endtime" : "null",
"t_startdate" : "null",
"t_enddate" : "null",
"i_timetype" : 0,
"i_dayofweek" : 0
}
]
}
]
}
]
}

-------------------------------------------

{
"i_width" : 1280,
"i_height" : 1024,
"layouts" : [{
"s_name" : "Unbenannt_1",
"i_duration" : 3000,
"t_starttime" : "null",
"t_endtime" : "null",
"t_startdate" : "null",
"t_enddate" : "null",
"i_dayofweek" : 0,
"i_timetype" : 0,
"frames" : [{
"s_name" : "Unbenannt",
"i_top" : 10,
"i_left" : 200,
"i_width" : 200,
"i_height" : 200,
"texts" : [{
"s_url" : "test.php",
"i_contenttype" : 1,
"s_html" : "{E52CD0B2-E4C1-4C7E-96FA-9C9E1275DAC8}", "s_name" : "Unbenannt",
"i_duration" : 5000,
"t_starttime" : "null",
"t_endtime" : "null",
"t_startdate" : "null",
"t_enddate" : "null",
"i_timetype" : 0,
"i_dayofweek" : 0
}]
}]
}]
}

Reply by email

Michael Van Canneyt
to theo
theo wrote
Thank you Michael, great! There is one little problem: the right square bracket which is closing an array always goes to the first position (no indentation).

Hm. This is a bug, I'll fix that. I didn't test arrays inside objects, only objects inside arrays...

And it's a matter of taste, if brackets and parenthesis should go to the same line.

Well, can provide an option for that, that's what they are for. Can you provide me an example of how NetBeans formats

a simple array [ 0, 1, 2 ]

and a simple object { "x" : 1, "y": 2 }

Just to compare.

I've attached an example. The FormatJSON output (above) and the NetBeans Formatter output (below).

I'll fix the bug, and if you can provide examples, I'll see about adding the options to create more "netbeans-compatible" formatted output.

Michael.

Reply by email

theo
Hm. This is a bug, I'll fix that. I didn't test arrays inside objects,

only

objects inside arrays...

Thanks

Can you provide me an example of how NetBeans formats a simple array [ 0, 1, 2 ]

It does nothing with these it seems. It leaves them as they were in the source code. If they had line breaks after the Comma, it won't remove them. Not even empty lines.

and a simple object { "x" : 1, "y": 2 }

Below are two examples formatted with Netbeans, your simple object and the wiki example.

SimpleObj={
"x" : 1,
"y": 2
};

WikiExample={
"Herausgeber": "Xema",
"Nummer": "1234-5678-9012-3456",
"Deckung": 2e+6,
"Währung": "EURO",
"Inhaber": {
"Name": "Mustermann",
"Vorname": "Max",
"männlich": true,
"Hobbys": [ "Reiten", "Golfen", "Lesen"],
"Alter": 42
}
};

It's not that I would say Netbeans is the reference, but what it does looks quite readable to me.
Not sure if not-formatting simple arrays is a good idea though.

Thank you.

Reply by email