Commit 89396ff02b

Techatrix <19954306+Techatrix@users.noreply.github.com>
2023-07-08 05:33:47
add `jsonParseFromValue` to `std.json.Value` (#16324)
1 parent b9fc0d2
Changed files (2)
lib/std/json/dynamic.zig
@@ -147,6 +147,12 @@ pub const Value = union(enum) {
             }
         }
     }
+
+    pub fn jsonParseFromValue(allocator: Allocator, source: Value, options: ParseOptions) !@This() {
+        _ = allocator;
+        _ = options;
+        return source;
+    }
 };
 
 fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, value_: Value) !?Value {
lib/std/json/dynamic_test.zig
@@ -245,6 +245,23 @@ test "Value.jsonStringify" {
     }
 }
 
+test "parseFromValue(std.json.Value,...)" {
+    const str =
+        \\{
+        \\  "int": 32,
+        \\  "float": 3.2,
+        \\  "str": "str",
+        \\  "array": [3, 2],
+        \\  "object": {}
+        \\}
+    ;
+
+    const parsed_tree = try parseFromSlice(Value, testing.allocator, str, .{});
+    defer parsed_tree.deinit();
+    const tree = try parseFromValueLeaky(Value, parsed_tree.arena.allocator(), parsed_tree.value, .{});
+    try testing.expect(std.meta.eql(parsed_tree.value, tree));
+}
+
 test "polymorphic parsing" {
     if (true) return error.SkipZigTest; // See https://github.com/ziglang/zig/issues/16108
     const doc =