authorgravatar for matthew.h.borkowski@gmail.comMatthew Borkowski <matthew.h.borkowski@gmail.com> 2021-05-13 09:43:55-04:00
committergravatar for matthew.h.borkowski@gmail.comMatthew Borkowski <matthew.h.borkowski@gmail.com> 2021-05-13 09:43:55-04:00
logcadb84b3ac891a99f7740be056d6bf6063b266f1
tree31e2dc83113f82ae3e26cf0424e8a109e52f016a
parente902c19c0e2bf7f0e9bc83b2c58d19ec024d56db

fix duplicate_field_behavior UseFirst in json.zig


1 files changed, 9 insertions(+), 2 deletions(-)

lib/std/json.zig+9-2
...@@ -1573,7 +1573,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:...@@ -1573,7 +1573,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
1573 // .UseLast => {},1573 // .UseLast => {},
1574 // }1574 // }
1575 if (options.duplicate_field_behavior == .UseFirst) {1575 if (options.duplicate_field_behavior == .UseFirst) {
1576 break;1576 // do nothing, check of fields_seen[i] below will parse value without overwriting field
1577 } else if (options.duplicate_field_behavior == .Error) {1577 } else if (options.duplicate_field_behavior == .Error) {
1578 return error.DuplicateJSONField;1578 return error.DuplicateJSONField;
1579 } else if (options.duplicate_field_behavior == .UseLast) {1579 } else if (options.duplicate_field_behavior == .UseLast) {
...@@ -1586,7 +1586,11 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:...@@ -1586,7 +1586,11 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
1586 return error.UnexpectedValue;1586 return error.UnexpectedValue;
1587 }1587 }
1588 } else {1588 } else {
1589 @field(r, field.name) = try parse(field.field_type, tokens, options);1589 if (fields_seen[i]) {
1590 parseFree(field.field_type, try parse(field.field_type, tokens, options), options);
1591 } else {
1592 @field(r, field.name) = try parse(field.field_type, tokens, options);
1593 }
1590 }1594 }
1591 fields_seen[i] = true;1595 fields_seen[i] = true;
1592 found = true;1596 found = true;
...@@ -2013,6 +2017,9 @@ test "parse into struct with duplicate field" {...@@ -2013,6 +2017,9 @@ test "parse into struct with duplicate field" {
20132017
2014 const T2 = struct { a: f64 };2018 const T2 = struct { a: f64 };
2015 try testing.expectEqual(T2{ .a = 0.25 }, try parse(T2, &TokenStream.init(str), options));2019 try testing.expectEqual(T2{ .a = 0.25 }, try parse(T2, &TokenStream.init(str), options));
2020 try testing.expectEqual(T2{ .a = 1.0 }, try parse(T2, &TokenStream.init(str),
2021 .{ .duplicate_field_behavior = .UseFirst }
2022 ));
2016}2023}
20172024
2018/// A non-stream JSON parser which constructs a tree of Value's.2025/// A non-stream JSON parser which constructs a tree of Value's.