| |
---|
| | import std.conv; |
---|
| | import std.datetime; |
---|
| | import std.exception; |
---|
| | import std.format; |
---|
| | import std.string; |
---|
| | import std.range; |
---|
| | import std.string : format; |
---|
| | import std.traits; |
---|
| | |
---|
| | version = JsonLineNumbers; |
---|
| | version = VibeJsonFieldNames; |
---|
| |
---|
| | */ |
---|
| | Json parseJson(R)(ref R range, int* line = null, string filename = null) |
---|
| | if( is(R == string) ) |
---|
| | { |
---|
| | import std.string : startsWith; |
---|
| | |
---|
| | Json ret; |
---|
| | enforceJson(!range.empty, "JSON string is empty.", filename, 0); |
---|
| | |
---|
| | skipWhitespace(range, line); |
---|
| |
---|
| | Throws a JSONException if any parsing error occurs. |
---|
| | */ |
---|
| | Json parseJsonString(string str, string filename = null) |
---|
| | { |
---|
| | import std.string : strip; |
---|
| | |
---|
| | auto strcopy = str; |
---|
| | int line = 0; |
---|
| | auto ret = parseJson(strcopy, &line, filename); |
---|
| | enforceJson(strcopy.strip().length == 0, "Expected end of string after JSON value.", filename, line); |
---|
| |
---|
| | assert(json.toPrettyString() == parseJsonString(json.toPrettyString()).toPrettyString()); |
---|
| | } |
---|
| | |
---|
| | unittest { |
---|
| | import std.string : endsWith; |
---|
| | |
---|
| | try parseJsonString(`{"a": 1`); |
---|
| | catch (Exception e) assert(e.msg.endsWith("Missing '}' before EOF.")); |
---|
| | try parseJsonString(`{"a": 1 x`); |
---|
| | catch (Exception e) assert(e.msg.endsWith("Expected '}' or ',' - got 'x'.")); |
---|
| |
---|
| | |