www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.json

reply AaronP <aaronbpaden gmail.com> writes:
Could I get a "hello, world" example of parsing json? The docs look 
simple enough, but I could still use an example.
Mar 25 2012
next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
Hope it's clear...


import std.json;
import std.stdio;

void main(string args[])
{
         JSONValue json = parseJSON(q"EOS
{
         "key" :
         {
                 "subkey1" : "str_val",
                 "subkey2" : [1,2,3],
                 "subkey3" : 3.1415
         }
}
EOS");
         writeln(json.object["key"].object["subkey1"].str);
         
writeln(json.object["key"].object["subkey2"].array[1].integer);
         writeln(json.object["key"].object["subkey3"].type == 
JSON_TYPE.FLOAT);
}


On Sunday, 25 March 2012 at 15:26:31 UTC, AaronP wrote:
 Could I get a "hello, world" example of parsing json? The docs 
 look simple enough, but I could still use an example.

Mar 25 2012
parent AaronP <aaronbpaden gmail.com> writes:
On 03/25/2012 12:50 PM, Andrea Fontana wrote:
 Hope it's clear...


 import std.json;
 import std.stdio;

 void main(string args[])
 {
 JSONValue json = parseJSON(q"EOS
 {
 "key" :
 {
 "subkey1" : "str_val",
 "subkey2" : [1,2,3],
 "subkey3" : 3.1415
 }
 }
 EOS");
 writeln(json.object["key"].object["subkey1"].str);
 writeln(json.object["key"].object["subkey2"].array[1].integer);
 writeln(json.object["key"].object["subkey3"].type == JSON_TYPE.FLOAT);
 }


 On Sunday, 25 March 2012 at 15:26:31 UTC, AaronP wrote:
 Could I get a "hello, world" example of parsing json? The docs look
 simple enough, but I could still use an example.


Ah. That's perfect, thanks. :)
Mar 25 2012
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/25/2012 08:26 AM, AaronP wrote:
 Could I get a "hello, world" example of parsing json? The docs look
 simple enough, but I could still use an example.

For what it's worth, I've just sent the following program to a friend before seeing this thread. 1) Save this sample text to a file named "json_file" { "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] } 2) The following program makes struct Employee objects from that file: import std.stdio; import std.json; import std.conv; import std.file; struct Employee { string firstName; string lastName; } void main() { // Assumes UTF-8 file auto content = to!string(read("json_file")); JSONValue[string] document = parseJSON(content).object; JSONValue[] employees = document["employees"].array; foreach (employeeJson; employees) { JSONValue[string] employee = employeeJson.object; string firstName = employee["firstName"].str; string lastName = employee["lastName"].str; auto e = Employee(firstName, lastName); writeln("Constructed: ", e); } } The output of the program: Constructed: Employee("John", "Doe") Constructed: Employee("Anna", "Smith") Constructed: Employee("Peter", "Jones") Ali
Mar 26 2012
next sibling parent reply "Alexsej" <lexa-skripa rambler,.ru> writes:
On Monday, 26 March 2012 at 07:14:50 UTC, Ali Çehreli wrote:
 On 03/25/2012 08:26 AM, AaronP wrote:
 Could I get a "hello, world" example of parsing json? The docs 
 look
 simple enough, but I could still use an example.

For what it's worth, I've just sent the following program to a friend before seeing this thread. 1) Save this sample text to a file named "json_file" { "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] } 2) The following program makes struct Employee objects from that file: import std.stdio; import std.json; import std.conv; import std.file; struct Employee { string firstName; string lastName; } void main() { // Assumes UTF-8 file auto content = to!string(read("json_file")); JSONValue[string] document = parseJSON(content).object; JSONValue[] employees = document["employees"].array; foreach (employeeJson; employees) { JSONValue[string] employee = employeeJson.object; string firstName = employee["firstName"].str; string lastName = employee["lastName"].str; auto e = Employee(firstName, lastName); writeln("Constructed: ", e); } } The output of the program: Constructed: Employee("John", "Doe") Constructed: Employee("Anna", "Smith") Constructed: Employee("Peter", "Jones") Ali

Your example only works if the json file in UTF-8 (BOM), how to make sure that it worked with the files in UTF-8 without BOM.
Jul 04 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/04/2012 08:25 AM, Alexsej wrote:
 On Monday, 26 March 2012 at 07:14:50 UTC, Ali Çehreli wrote:

 // Assumes UTF-8 file
 auto content = to!string(read("json_file"));


 Your example only works if the json file in UTF-8 (BOM), how to make
 sure that it worked with the files in UTF-8 without BOM.

I am pretty sure that I have tested it without BOM, which is not recommended nor needed for UTF-8 anyway. If anything, the file should not have any BOM because it is being converted to a string and is being passed to parseJSON(). I don't think parseJSON() expects a BOM either. Does it? Ali
Jul 04 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/04/2012 10:51 AM, nazriel wrote:

 Ali, I allowed myself to copy-modify.a.little-paste your example to
 dpaste.dzfl.pl

 Hope you don't mind!
 Here it is http://dpaste.dzfl.pl/a76157cf

No problem! Thanks for doing that. :) Ali
Jul 04 2012
prev sibling parent "nazriel" <nazriel6969 gmail.com> writes:
On Wednesday, 4 July 2012 at 16:55:19 UTC, Ali Çehreli wrote:
 On 07/04/2012 08:25 AM, Alexsej wrote:
 On Monday, 26 March 2012 at 07:14:50 UTC, Ali Çehreli wrote:

 // Assumes UTF-8 file
 auto content = to!string(read("json_file"));


 Your example only works if the json file in UTF-8 (BOM), how

 sure that it worked with the files in UTF-8 without BOM.

I am pretty sure that I have tested it without BOM, which is not recommended nor needed for UTF-8 anyway. If anything, the file should not have any BOM because it is being converted to a string and is being passed to parseJSON(). I don't think parseJSON() expects a BOM either. Does it? Ali

Ali, I allowed myself to copy-modify.a.little-paste your example to dpaste.dzfl.pl Hope you don't mind! Here it is http://dpaste.dzfl.pl/a76157cf
Jul 04 2012
prev sibling next sibling parent "Vincent" <thornik gmail.com> writes:
On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote:
 Hope it's clear...

Nope, it's something like chess and have nothing common with simplicity of the real JSON usage! This is example from C#: var p = JsonConvert.DeserializeObject<Person>("{some real JSON, not crapy EOS}"); var str = JsonConvert.SerializeObject(p); That's it! And this is how it SHOULD be implemented. Cannot catch why this stupid realization came to standard library... :((
May 17 2012
prev sibling next sibling parent "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> writes:
On Thursday, 17 May 2012 at 14:08:27 UTC, Vincent wrote:
 On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote:
 Hope it's clear...

Nope, it's something like chess and have nothing common with simplicity of the real JSON usage! This is example from C#: var p = JsonConvert.DeserializeObject<Person>("{some real JSON, not crapy EOS}"); var str = JsonConvert.SerializeObject(p); That's it! And this is how it SHOULD be implemented. Cannot catch why this stupid realization came to standard library... :((

I'm pretty new to D, but I am an expert Java developer, self claimed. I am fluent in many other languages as well. In all languages there is a basis documentation. Read the documentation for parseJSON and you'll see that it should be possible to send in a straight JSON string. I think the complex example is a bit stupid. It scares developers away from the lang. Feel free to correct me of course.
May 17 2012
prev sibling next sibling parent "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> writes:
On Thursday, 17 May 2012 at 18:36:22 UTC, Jarl André wrote:
 On Thursday, 17 May 2012 at 14:08:27 UTC, Vincent wrote:
 On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote:
 Hope it's clear...

Nope, it's something like chess and have nothing common with simplicity of the real JSON usage! This is example from C#: var p = JsonConvert.DeserializeObject<Person>("{some real JSON, not crapy EOS}"); var str = JsonConvert.SerializeObject(p); That's it! And this is how it SHOULD be implemented. Cannot catch why this stupid realization came to standard library... :((

I'm pretty new to D, but I am an expert Java developer, self claimed. I am fluent in many other languages as well. In all languages there is a basis documentation. Read the documentation for parseJSON and you'll see that it should be possible to send in a straight JSON string. I think the complex example is a bit stupid. It scares developers away from the lang. Feel free to correct me of course.

The final proof of exisiting simplicity :) JSONValue[string] value = parseJSON("{ \"test\": \"1\" }").object; writeln(value["test"].str); This outputs "1"
May 17 2012
prev sibling parent "Tim Shea" <tim.m.shea gmail.com> writes:
On Thursday, 17 May 2012 at 18:55:57 UTC, Jarl André wrote:
 On Thursday, 17 May 2012 at 18:36:22 UTC, Jarl André wrote:
 On Thursday, 17 May 2012 at 14:08:27 UTC, Vincent wrote:
 On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana 
 wrote:
 Hope it's clear...

Nope, it's something like chess and have nothing common with simplicity of the real JSON usage! This is example from C#: var p = JsonConvert.DeserializeObject<Person>("{some real JSON, not crapy EOS}"); var str = JsonConvert.SerializeObject(p); That's it! And this is how it SHOULD be implemented. Cannot catch why this stupid realization came to standard library... :((

I'm pretty new to D, but I am an expert Java developer, self claimed. I am fluent in many other languages as well. In all languages there is a basis documentation. Read the documentation for parseJSON and you'll see that it should be possible to send in a straight JSON string. I think the complex example is a bit stupid. It scares developers away from the lang. Feel free to correct me of course.

The final proof of exisiting simplicity :) JSONValue[string] value = parseJSON("{ \"test\": \"1\" }").object; writeln(value["test"].str); This outputs "1"

Vincent, I'm not sure what you are trying to claim by saying "some real JSON, not crappy EOS", all modern languages support special string delimiters to avoid escaping characters. This isn't a deficit it's a feature which improves readability. On the other hand, Phobos does (by design) tend towards minimalism, this is a legitimate library strategy, as it allows me to serialize and deserialize objects in whatever way suits me. Unfortunately it has the side effect that you don't get to have a built in function for every common task. It's a tradeoff. Anyway, I just wanted to ask if we could get Ali or Jarl's (or both) samples added to the documentation. I read the docs, and having only worked with JSON in Javascript and PHP, wasn't quite clear how to use std.json. As it turns out, it's exactly what one would expect :D
Jun 24 2012