Skip to content

Instantly share code, notes, and snippets.

@bithavoc
Last active October 21, 2016 13:15
Show Gist options
  • Select an option

  • Save bithavoc/8686903 to your computer and use it in GitHub Desktop.

Select an option

Save bithavoc/8686903 to your computer and use it in GitHub Desktop.
Demo of Parsing objects using instances of JSONValue
/*
* http://johan.heapsource.com
*/
import std.stdio;
import std.json;
JSONValue doc;
void main() {
parseMyDoc();
printMyDoc();
}
void parseMyDoc() {
JSONValue v = parseJSON(q{
{"a":2, "b":{"c":20}}
});
doc = v;
}
void printMyDoc() {
string str = toJSON(&doc, true);
writeln(str);
}
// (Output)
/*
{
"a": 2,
"b": {
"c": 20
}
}
*/
/*
* http://johan.heapsource.com
*/
import std.stdio;
import std.json;
JSONValue * doc;
void main() {
parseMyDoc();
printMyDoc();
}
void parseMyDoc() {
JSONValue v = parseJSON(q{
{"a":2, "b":{"c":20}}
});
doc = &v;
}
void printMyDoc() {
string str = toJSON(doc, true);
writeln(str);
}
// (Undefined behavior or Segmentation Fault)
/*
* http://johan.heapsource.com
*/
import std.stdio;
import std.json;
void main() {
JSONValue * v = new JSONValue;
*v = parseJSON(q{
{"a":2, "b":{"c":20}}
});
string str = v.toJSON(true);
writeln(str);
}
// (Output)
/*
{
"a": 2,
"b": {
"c": 20
}
}
*/
/*
* http://johan.heapsource.com
*/
import std.stdio;
import std.json;
void main() {
JSONValue v = parseJSON(q{
{"a":2, "b":{"c":20}}
});
string str = toJSON(&v, true);
writeln(str);
}
// (Output)
/*
{
"a": 2,
"b": {
"c": 20
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment