Skip to content

Commit

Permalink
Add support for parsing and generating JSON
Browse files Browse the repository at this point in the history
This adds the std::json module, providing the means to parse JSON
strings and generate JSON strings from Inko objects. When parsing a JSON
string, the result is a std::json::Json value. These values in turn can
be converted (back) to JSON using Json.to_string and
Json.to_pretty_string.

The JSON parser is compliant with RFC 8259 and behaves as expected per
the tests from https://seriot.ch/projects/parsing_json.html. The parser
doesn't use a tokenizer as there's no need for one, and this reduces the
amount of allocations the parser needs.

This fixes https://gitlab.com/inko-lang/inko/-/issues/290.

Changelog: added
  • Loading branch information
yorickpeterse committed Oct 8, 2022
1 parent 8bc9613 commit 09d8178
Show file tree
Hide file tree
Showing 7 changed files with 1,263 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
2 changes: 1 addition & 1 deletion libstd/src/std/float.inko
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class builtin Float {
#
# Float.parse('1.2e1') # => Option.Some(12.0)
fn pub static parse(string: ref String) -> Option[Self] {
let res = _INKO.string_to_float(string)
let res = _INKO.string_to_float(string, -1, -1)

if _INKO.is_undefined(res) { Option.None } else { Option.Some(res) }
}
Expand Down
6 changes: 3 additions & 3 deletions libstd/src/std/int.inko
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class builtin Int {
# Int.from_base2('11') # => Option.Some(3)
# Int.from_base2('ff') # => Option.None
fn pub static from_base2(string: ref String) -> Option[Self] {
let res = _INKO.string_to_int(string, 2)
let res = _INKO.string_to_int(string, 2, -1, -1)

if _INKO.is_undefined(res) { Option.None } else { Option.Some(res) }
}
Expand All @@ -57,7 +57,7 @@ class builtin Int {
# Int.from_base10('12') # => Option.Some(12)
# Int.from_base10('ff') # => Option.None
fn pub static from_base10(string: ref String) -> Option[Self] {
let res = _INKO.string_to_int(string, 10)
let res = _INKO.string_to_int(string, 10, -1, -1)

if _INKO.is_undefined(res) { Option.None } else { Option.Some(res) }
}
Expand All @@ -76,7 +76,7 @@ class builtin Int {
# Int.from_base16('ef') # => Option.Some(239)
# Int.from_base16('zz') # => Option.None
fn pub static from_base16(string: ref String) -> Option[Self] {
let res = _INKO.string_to_int(string, 16)
let res = _INKO.string_to_int(string, 16, -1, -1)

if _INKO.is_undefined(res) { Option.None } else { Option.Some(res) }
}
Expand Down
Loading

0 comments on commit 09d8178

Please sign in to comment.