From: Brendan Hansen Date: Mon, 14 Jun 2021 19:54:22 +0000 (-0500) Subject: started working on a json library X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=7e8c65dbd02fb4dcd7b7bb5a1a3d2a921c754667;p=onyx.git started working on a json library --- diff --git a/modules/json/decoder.onyx b/modules/json/decoder.onyx new file mode 100644 index 00000000..8d6443ed --- /dev/null +++ b/modules/json/decoder.onyx @@ -0,0 +1,11 @@ +package json +use package core + +decode_string :: (data: str, allocator := context.allocator) -> Value { + stream := io.string_stream_make(data); + return decode(^stream, allocator); +} + +decode :: (stream: ^io.Stream, allocator := context.allocator) -> Value { + +} \ No newline at end of file diff --git a/modules/json/encoder.onyx b/modules/json/encoder.onyx new file mode 100644 index 00000000..09d59843 --- /dev/null +++ b/modules/json/encoder.onyx @@ -0,0 +1 @@ +package json \ No newline at end of file diff --git a/modules/json/example.onyx b/modules/json/example.onyx new file mode 100644 index 00000000..04468ece --- /dev/null +++ b/modules/json/example.onyx @@ -0,0 +1,17 @@ + +#load "core/std" +#load "modules/json/module" + +use package core +json :: package json + +json_string := "{ \"test\": \"Hello, World!\", \"array\": [1,2,3,4,5], }"; + +main :: (args: [] cstr) { + decoded_json := json.decode_string(json_string); + defer json.free(decoded_json); + + test_str := decoded_json |> json.get("test") + |> json.to_str(); + println(test_str); +} \ No newline at end of file diff --git a/modules/json/module.onyx b/modules/json/module.onyx new file mode 100644 index 00000000..6d8d8ceb --- /dev/null +++ b/modules/json/module.onyx @@ -0,0 +1,7 @@ + + + +package json + +#load "./encoder" +#load "./decoder" \ No newline at end of file diff --git a/modules/json/types.onyx b/modules/json/types.onyx new file mode 100644 index 00000000..cec32eff --- /dev/null +++ b/modules/json/types.onyx @@ -0,0 +1,25 @@ +package json + +use package core.map { Map } + +Value :: struct { + Type :: enum { + Null; + Bool; + Integer; + Float; + String; + Array; + Object; + } + + type : Type; + value : struct #union { + bool_ : bool; + int_ : i64; + float_ : f64; + str_ : str; + array_ : [..] Value; + object_ : Map(str, Value); + } +} \ No newline at end of file