From: Brendan Hansen Date: Sat, 19 Jun 2021 01:06:56 +0000 (-0500) Subject: testing json module X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c7b8bb34599108b09327bfc08c5a00336e9cb9c7;p=onyx.git testing json module --- diff --git a/modules/json/encoder.onyx b/modules/json/encoder.onyx index eb08922b..df5dd013 100644 --- a/modules/json/encoder.onyx +++ b/modules/json/encoder.onyx @@ -114,8 +114,8 @@ encode :: #match { }, - // Inserted after any of the #add_match directives - #precedence 1000 (w: ^io.Writer, v: $T) -> Encoding_Error { - return .Unsupported_Type; - } + // // Inserted after any of the #add_match directives + // #precedence 1000 (w: ^io.Writer, v: $T) -> Encoding_Error { + // return .Unsupported_Type; + // } } \ No newline at end of file diff --git a/modules/json/example.onyx b/modules/json/example.onyx index d45297c4..37db4715 100644 --- a/modules/json/example.onyx +++ b/modules/json/example.onyx @@ -31,7 +31,28 @@ main :: (args: [] cstr) { decoded_json.root["test"]->as_str() |> println(); } - json.encode(^stdio.print_writer, decoded_json.root); + json.encode(^stdio.print_writer, decoded_json.root["sub"]); + print("\n"); + + v_arr := array.make(Vector2); + defer array.free(^v_arr); + + array.push(^v_arr, .{ 10, 20 }); + array.push(^v_arr, .{ 12.34, 45.67 }); + array.push(^v_arr, .{ 0, -10 }); + + if err := json.encode(^stdio.print_writer, v_arr); err != .None { + printf("Error formatting json %i", cast(i32) err); + } print("\n"); } - \ No newline at end of file + +Vector2 :: struct { + x, y: f32; +} + +#add_match json.encode, (w: ^io.Writer, v: Vector2) -> json.Encoding_Error { + io.write_format(w, "{\"x\":%f,\"y\":%f}", v.x, v.y); + + return .None; +} \ No newline at end of file