| ... | @@ -743,17 +743,13 @@ pub fn stringify(writer: anytype, value: anytype, options: std.json.Stringify.Op | ... | @@ -743,17 +743,13 @@ pub fn stringify(writer: anytype, value: anytype, options: std.json.Stringify.Op |
| 743 | try writer.writeAll("\""); | 743 | try writer.writeAll("\""); |
| 744 | } | 744 | } |
| 745 | } else { | 745 | } else { |
| 746 | return error.Unexpected; | 746 | var view = std.unicode.Utf8View.init(value) catch return error.Unexpected; |
| 747 | } | 747 | var iter = view.iterator(); |
| 748 | return; | 748 | try writer.writeAll("\""); |
| 749 | } | 749 | while (iter.nextCodepointSlice()) |sl| { |
| 750 | if (comptime extras.isArrayOf(u8)(T)) { | 750 | const cp = std.unicode.utf8Decode(sl) catch unreachable; |
| 751 | if (extras.matchesAll(u8, &value, std.ascii.isAscii)) { | 751 | if (cp < 128) { |
| 752 | if (extras.matchesAll(u8, &value, std.ascii.isPrint)) { | 752 | const c: u8 = @intCast(cp); |
| 753 | try writer.writevAll(&.{ &.{'"'}, &value, &.{'"'} }); | | |
| 754 | } else { | | |
| 755 | try writer.writeAll("\""); | | |
| 756 | for (&value) |c| { | | |
| 757 | try writer.writeAll(switch (c) { | 753 | try writer.writeAll(switch (c) { |
| 758 | 0x08 => "\\b", | 754 | 0x08 => "\\b", |
| 759 | 0x09 => "\\t", | 755 | 0x09 => "\\t", |
| ... | @@ -765,14 +761,18 @@ pub fn stringify(writer: anytype, value: anytype, options: std.json.Stringify.Op | ... | @@ -765,14 +761,18 @@ pub fn stringify(writer: anytype, value: anytype, options: std.json.Stringify.Op |
| 765 | 0x23...0x7e => &.{c}, | 761 | 0x23...0x7e => &.{c}, |
| 766 | else => "\\u00" ++ extras.to_hex([_]u8{c}), | 762 | else => "\\u00" ++ extras.to_hex([_]u8{c}), |
| 767 | }); | 763 | }); |
| | 764 | continue; |
| 768 | } | 765 | } |
| 769 | try writer.writeAll("\""); | 766 | try writer.writeAll(sl); |
| 770 | } | 767 | } |
| 771 | } else { | 768 | try writer.writeAll("\""); |
| 772 | return error.Unexpected; | 769 | return; |
| 773 | } | 770 | } |
| 774 | return; | 771 | return; |
| 775 | } | 772 | } |
| | 773 | if (comptime extras.isArrayOf(u8)(T)) { |
| | 774 | return stringify(writer, &value, options); |
| | 775 | } |
| 776 | if (comptime extras.isSlice(T)) { | 776 | if (comptime extras.isSlice(T)) { |
| 777 | try writer.writeAll("["); | 777 | try writer.writeAll("["); |
| 778 | for (value, 0..) |item, i| { | 778 | for (value, 0..) |item, i| { |