digitalmars.D.learn - std.json
- AaronP (2/2) Mar 25 2012 Could I get a "hello, world" example of parsing json? The docs look
- Andrea Fontana (22/24) Mar 25 2012 Hope it's clear...
- AaronP (2/24) Mar 25 2012 Ah. That's perfect, thanks. :)
- Vincent (8/9) May 17 2012 Nope, it's something like chess and have nothing common with
- "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> (9/19) May 17 2012 I'm pretty new to D, but I am an expert Java developer, self
- "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> (6/28) May 17 2012 The final proof of exisiting simplicity :)
- Tim Shea (15/47) Jun 24 2012 Vincent, I'm not sure what you are trying to claim by saying
- jean christophe (2/34) Nov 13 2013 +1
- Shammah Chancellor (8/20) Nov 13 2013 This is planned in the upgrade to std.json which is currently being
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (40/42) Mar 26 2012 For what it's worth, I've just sent the following program to a friend
- Alexsej (3/47) Jul 04 2012 Your example only works if the json file in UTF-8 (BOM), how to
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/12) Jul 04 2012 I am pretty sure that I have tested it without BOM, which is not
- nazriel (5/18) Jul 04 2012 Ali, I allowed myself to copy-modify.a.little-paste your example
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/7) Jul 04 2012 No problem! Thanks for doing that. :)
- Rob T (5/18) Nov 14 2013 Last time I checked, parseJSON will fail if a BOM exists. Now
- bearophile (5/7) Nov 13 2013 Is this better/worse?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/10) Nov 13 2013 That is much better.
- Craig Dillabaugh (64/108) Nov 20 2013 So I was thinking of adding an example to the std.json documents,
- Dicebot (5/5) Nov 20 2013 For tasks that imply conversion between D types and JSON text
- Craig Dillabaugh (6/11) Nov 20 2013 I absolutely agree. However, I wanted to come up with an example
- Dicebot (4/4) Nov 20 2013 What I mean is that std.json does not seem to be written with
- Craig Dillabaugh (3/7) Nov 20 2013 So I was basically wasting my time trying to figure out how to
- Orvid King (7/14) Nov 20 2013 But driving a nail with a screwdriver works very well! Or at least it
- Craig Dillabaugh (3/23) Nov 20 2013 Thanks, I may have a look.
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
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
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:Ah. That's perfect, thanks. :)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
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 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
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: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.Hope it's clear...Nope, it's something like chess and have nothing common with 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
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:The final proof of exisiting simplicity :) JSONValue[string] value = parseJSON("{ \"test\": \"1\" }").object; writeln(value["test"].str); This outputs "1"On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote: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.Hope it's clear...Nope, it's something like chess and have nothing common with 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
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: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 :DOn Thursday, 17 May 2012 at 14:08:27 UTC, Vincent wrote:The final proof of exisiting simplicity :) JSONValue[string] value = parseJSON("{ \"test\": \"1\" }").object; writeln(value["test"].str); This outputs "1"On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote: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.Hope it's clear...Nope, it's something like chess and have nothing common with 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... :((
Jun 24 2012
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:+1On Thursday, 17 May 2012 at 14:08:27 UTC, Vincent wrote:The final proof of exisiting simplicity :) JSONValue[string] value = parseJSON("{ \"test\": \"1\" }").object; writeln(value["test"].str); This outputs "1"On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote: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.Hope it's clear...Nope, it's something like chess and have nothing common with 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... :((
Nov 13 2013
On 2012-05-17 14:08:26 +0000, Vincent said:On Sunday, 25 March 2012 at 17:50:45 UTC, Andrea Fontana wrote:This is planned in the upgrade to std.json which is currently being developed. However, you'll note that you cannot parse generic JSON that way ( since you need a defined type "Person"). The current parseJSON is really a different feature from what exists in your a similar feature to deserialize JSON into ExpandoObjects, but that's also different from your example.Hope it's clear...Nope, it's something like chess and have nothing common with simplicity 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... :((
Nov 13 2013
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
On Monday, 26 March 2012 at 07:14:50 UTC, Ali Çehreli wrote:On 03/25/2012 08:26 AM, AaronP wrote: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.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
Jul 04 2012
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
On Wednesday, 4 July 2012 at 16:55:19 UTC, Ali Çehreli wrote:On 07/04/2012 08:25 AM, Alexsej 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/a76157cfOn 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), howto makesure 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
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/a76157cfNo problem! Thanks for doing that. :) Ali
Jul 04 2012
On Wednesday, 4 July 2012 at 16:55:19 UTC, Ali Çehreli wrote:On 07/04/2012 08:25 AM, Alexsej wrote:Last time I checked, parseJSON will fail if a BOM exists. Now that I think of it, maybe that state of affairs should be filed as a bug or feature enhancement. --rtOn 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), howto makesure 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
Nov 14 2013
Ali Çehreli:// Assumes UTF-8 file auto content = to!string(read("json_file"));Is this better/worse? auto content = "json_file".readText; Bye, bearophile
Nov 13 2013
On 11/13/2013 01:47 AM, bearophile wrote:Ali Çehreli:That is much better. Ali// Assumes UTF-8 file auto content = to!string(read("json_file"));Is this better/worse? auto content = "json_file".readText; Bye, bearophile
Nov 13 2013
On Monday, 26 March 2012 at 07:14:50 UTC, Ali Çehreli wrote:On 03/25/2012 08:26 AM, AaronP wrote:So I was thinking of adding an example to the std.json documents, and was going to ask to rip-off Ali's example here. However, I thought I would be nice to have an example going the other way (ie. taking some data structure and converting to JSON format). I came up with the following using Ali's Employee struct: /** * Generate a JSON string from an array of employees using std.json, * even though the code to generate the same by hand would be shorter * and easier to follow :o) */ string employeesToJSON( Employee[] employees ) { JSONValue emp_array; emp_array.type = JSON_TYPE.ARRAY; emp_array.array = []; foreach( e; employees ) { JSONValue emp_object; emp_object.type = JSON_TYPE.OBJECT; emp_object.object = null; JSONValue first_name; first_name.str = e.firstName; first_name.type = JSON_TYPE.STRING; JSONValue last_name; last_name.str = e.lastName; last_name.type = JSON_TYPE.STRING; emp_object.object["firstName"] = first_name; emp_object.object["lastName"] = last_name; emp_array.array ~= emp_object; } JSONValue root; root.type = JSON_TYPE.OBJECT; root.object[""] = emp_array; return toJSON( &root ); } Then if I call it using the following code: Employee[] employees = [ { "Walter", "Bright" }, { "Andrei", "Alexandrescu"}, { "Celine", "Dion" } ]; writeln( employeesToJSON( employees ) ); It prints out: {"":[{"lastName":"Bright","firstName":"Walter"},{"lastName":"Alexandrescu","firstName":"Andrei"},{"lastName":"Dion","firstName":"Celine"}]} Which isn't exactly what I want as I have the extra "" at the start. So I have two questions: 1. Is there a nicer way to generate my JSONValue tree. 2. How do I insert my JSONValue.array of employees into my root JSONValue. I tried: root.object[""] = emp_array; // generates { "": [ ... } root.object[null] = emp_array; // generates { "": [ ... } root.object = emp_array; //Syntax error //Error: cannot implicitly convert expression //(emp_array) of type JSONValue to JSONValue[string] I want my returned string as { [ ... ] } Cheers, CraigCould 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
Nov 20 2013
For tasks that imply conversion between D types and JSON text (alike to serialization), vibe.d module is really much superior because it provides functions like http://vibed.org/api/vibe.data.json/serializeToJson which "just work" on many user types.
Nov 20 2013
On Wednesday, 20 November 2013 at 13:20:48 UTC, Dicebot wrote:For tasks that imply conversion between D types and JSON text (alike to serialization), vibe.d module is really much superior because it provides functions like http://vibed.org/api/vibe.data.json/serializeToJson which "just work" on many user types.I absolutely agree. However, I wanted to come up with an example for std.json, and this mess was the best I could do. I was curious if someone could come up with a nicer mess than mine. Also for small jobs it might be preferable to use std.json - if you don't want the vibe.d dependency.
Nov 20 2013
What I mean is that std.json does not seem to be written with such usage mode in mind, it is more about direct DOM manipulation/construction. So probably examples should probably not highlight how bad it is at tasks it is really bad at :P
Nov 20 2013
On Wednesday, 20 November 2013 at 13:29:54 UTC, Dicebot wrote:What I mean is that std.json does not seem to be written with such usage mode in mind, it is more about direct DOM manipulation/construction. So probably examples should probably not highlight how bad it is at tasks it is really bad at :PSo I was basically wasting my time trying to figure out how to drive a nail with a screwdriver :o)
Nov 20 2013
On 11/20/13, Craig Dillabaugh <cdillaba cg.scs.carleton.ca> wrote:On Wednesday, 20 November 2013 at 13:29:54 UTC, Dicebot wrote:But driving a nail with a screwdriver works very well! Or at least it does if you have a large enough screwdriver :P Regardless, if your looking to avoid the vibe.d dependency, my serialization framework does support JSON, and still has the simple API that vibe.d's module does. https://github.com/Orvid/JSONSerializationWhat I mean is that std.json does not seem to be written with such usage mode in mind, it is more about direct DOM manipulation/construction. So probably examples should probably not highlight how bad it is at tasks it is really bad at :PSo I was basically wasting my time trying to figure out how to drive a nail with a screwdriver :o)
Nov 20 2013
On Wednesday, 20 November 2013 at 13:48:37 UTC, Orvid King wrote:On 11/20/13, Craig Dillabaugh <cdillaba cg.scs.carleton.ca> wrote:Thanks, I may have a look. CraigOn Wednesday, 20 November 2013 at 13:29:54 UTC, Dicebot wrote:But driving a nail with a screwdriver works very well! Or at least it does if you have a large enough screwdriver :P Regardless, if your looking to avoid the vibe.d dependency, my serialization framework does support JSON, and still has the simple API that vibe.d's module does. https://github.com/Orvid/JSONSerializationWhat I mean is that std.json does not seem to be written with such usage mode in mind, it is more about direct DOM manipulation/construction. So probably examples should probably not highlight how bad it is at tasks it is really bad at :PSo I was basically wasting my time trying to figure out how to drive a nail with a screwdriver :o)
Nov 20 2013