changed std.json.ObjectMap/Array to unmanaged defaults

Having ObjectMap point to object_unmanaged, could be confusing and that
way it should be more compatible with existing code.
This commit is contained in:
Daniel Wojcik 2025-10-29 00:42:55 +01:00
parent 16bd318e4f
commit 6dc2577f14
4 changed files with 17 additions and 19 deletions

View file

@ -60,6 +60,8 @@ test Stringify {
try testing.expectEqualSlices(u8, expected, out.written());
}
pub const ObjectMapManaged = @import("json/dynamic.zig").ObjectMapManaged;
pub const ArrayManaged = @import("json/dynamic.zig").ArrayManaged;
pub const ObjectMap = @import("json/dynamic.zig").ObjectMap;
pub const Array = @import("json/dynamic.zig").Array;
pub const Value = @import("json/dynamic.zig").Value;

View file

@ -770,11 +770,10 @@ fn testBasicWriteStream(w: *Stringify) !void {
}
fn getJsonObject(allocator: std.mem.Allocator) !std.json.Value {
const obj = try allocator.create(std.json.ObjectMap);
obj.* = std.json.ObjectMap.init(allocator);
var v: std.json.Value = .{ .object_managed = obj };
try v.object_managed.put("one", std.json.Value{ .integer = @as(i64, @intCast(1)) });
try v.object_managed.put("two", std.json.Value{ .float = 2.0 });
const obj = std.json.ObjectMap.empty;
var v: std.json.Value = .{ .object = obj };
try v.object.put(allocator, "one", std.json.Value{ .integer = @as(i64, @intCast(1)) });
try v.object.put(allocator, "two", std.json.Value{ .float = 2.0 });
return v;
}

View file

@ -10,15 +10,12 @@ const ParseError = @import("./static.zig").ParseError;
const isNumberFormattedLikeAnInteger = @import("Scanner.zig").isNumberFormattedLikeAnInteger;
pub const ObjectMapUnmanaged = std.StringArrayHashMapUnmanaged(Value);
pub const ArrayUnmanaged = std.ArrayList(Value);
pub const ObjectMap = std.StringArrayHashMapUnmanaged(Value);
pub const Array = std.ArrayList(Value);
pub const ObjectMapManaged = StringArrayHashMap(Value);
pub const ArrayManaged = std.array_list.Managed(Value);
pub const ObjectMap = ObjectMapManaged;
pub const Array = ArrayManaged;
/// Represents any JSON value, potentially containing other JSON values.
/// A .float value may be an approximation of the original value.
/// Arbitrary precision numbers can be represented by .number_string values.
@ -30,8 +27,8 @@ pub const Value = union(enum) {
float: f64,
number_string: []const u8,
string: []const u8,
array: ArrayUnmanaged,
object: ObjectMapUnmanaged,
array: Array,
object: ObjectMap,
array_managed: *ArrayManaged,
object_managed: *ObjectMapManaged,
@ -124,7 +121,7 @@ pub const Value = union(enum) {
.object_begin => {
switch (try source.nextAllocMax(allocator, .alloc_always, options.max_value_len.?)) {
.object_end => return try handleCompleteValue(&stack, allocator, source, Value{ .object = ObjectMapUnmanaged.empty }, options) orelse continue,
.object_end => return try handleCompleteValue(&stack, allocator, source, Value{ .object = ObjectMap.empty }, options) orelse continue,
.allocated_string => |key| {
try stack.appendSlice(&[_]Value{
Value{ .object = .empty },

View file

@ -225,10 +225,10 @@ test "Value.jsonStringify" {
.{ .integer = 2 },
.{ .number_string = "3" },
};
var obj = ObjectMap.init(testing.allocator);
defer obj.deinit();
try obj.putNoClobber("a", .{ .string = "b" });
var arr = Array.fromOwnedSlice(undefined, &vals);
const allocator = testing.allocator;
var obj = ObjectMap.empty;
defer obj.deinit(allocator);
try obj.putNoClobber(allocator, "a", .{ .string = "b" });
const array = [_]Value{
.null,
.{ .bool = true },
@ -236,8 +236,8 @@ test "Value.jsonStringify" {
.{ .number_string = "43" },
.{ .float = 42 },
.{ .string = "weeee" },
.{ .array_managed = &arr },
.{ .object_managed = &obj },
.{ .array = Array.fromOwnedSlice(&vals) },
.{ .object = obj },
};
var buffer: [0x1000]u8 = undefined;
var fixed_writer: Writer = .fixed(&buffer);