Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit ab203b0

Browse files
committed
support writing for json 5 compatible format
1 parent c5ba452 commit ab203b0

8 files changed

Lines changed: 348 additions & 95 deletions

File tree

source/JSON/IJSONSerializable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
public interface IJSONSerializable
44
{
55
void Read(JSONReader reader);
6-
void Write(JSONWriter writer);
6+
void Write(ref JSONWriter writer);
77
}
88
}

source/JSON/JSONArray.cs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,22 @@ public void Dispose()
7272
MemoryAddress.Free(ref jsonArray);
7373
}
7474

75-
public readonly void ToString(Text result, ReadOnlySpan<char> indent = default, bool cr = false, bool lf = false, byte depth = 0)
75+
public readonly void ToString(Text result, SerializationSettings settings = default)
76+
{
77+
ToString(result, settings, 0);
78+
}
79+
80+
internal readonly void ToString(Text result, SerializationSettings settings, byte depth)
7681
{
7782
ThrowIfDisposed();
7883

7984
result.Append('[');
8085
if (jsonArray->elements.Count > 0)
8186
{
82-
NewLine();
87+
settings.NewLine(result);
8388
for (int i = 0; i <= depth; i++)
8489
{
85-
Indent(indent);
90+
settings.Indent(result);
8691
}
8792

8893
int position = 0;
@@ -91,7 +96,7 @@ public readonly void ToString(Text result, ReadOnlySpan<char> indent = default,
9196
ref JSONProperty element = ref jsonArray->elements[position];
9297
byte childDepth = depth;
9398
childDepth++;
94-
element.ToString(result, false, indent, cr, lf, childDepth);
99+
element.ToString(result, settings, childDepth);
95100
position++;
96101

97102
if (position == Count)
@@ -100,39 +105,21 @@ public readonly void ToString(Text result, ReadOnlySpan<char> indent = default,
100105
}
101106

102107
result.Append(',');
103-
NewLine();
108+
settings.NewLine(result);
104109
for (int i = 0; i <= depth; i++)
105110
{
106-
Indent(indent);
111+
settings.Indent(result);
107112
}
108113
}
109114

110-
NewLine();
115+
settings.NewLine(result);
111116
for (int i = 0; i < depth; i++)
112117
{
113-
Indent(indent);
118+
settings.Indent(result);
114119
}
115120
}
116121

117122
result.Append(']');
118-
119-
void NewLine()
120-
{
121-
if (cr)
122-
{
123-
result.Append('\r');
124-
}
125-
126-
if (lf)
127-
{
128-
result.Append('\n');
129-
}
130-
}
131-
132-
void Indent(ReadOnlySpan<char> indent)
133-
{
134-
result.Append(indent);
135-
}
136123
}
137124

138125
public readonly override string ToString()

source/JSON/JSONObject.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Diagnostics;
44
using System.Runtime.CompilerServices;
5-
using System.Text.Json.Nodes;
65
using Unmanaged;
76

87
namespace Serialization.JSON
@@ -150,17 +149,22 @@ public readonly T As<T>() where T : unmanaged, IJSONSerializable
150149
return value;
151150
}
152151

153-
public readonly void ToString(Text result, ReadOnlySpan<char> indent = default, bool cr = false, bool lf = false, byte depth = 0)
152+
public readonly void ToString(Text result, SerializationSettings settings = default)
153+
{
154+
ToString(result, settings, 0);
155+
}
156+
157+
internal readonly void ToString(Text result, SerializationSettings settings, byte depth)
154158
{
155159
ThrowIfDisposed();
156160

157161
result.Append('{');
158162
if (jsonObject->properties.Count > 0)
159163
{
160-
NewLine();
164+
settings.NewLine(result);
161165
for (int i = 0; i <= depth; i++)
162166
{
163-
Indent(indent);
167+
settings.Indent(result);
164168
}
165169

166170
int position = 0;
@@ -169,7 +173,11 @@ public readonly void ToString(Text result, ReadOnlySpan<char> indent = default,
169173
ref JSONProperty property = ref jsonObject->properties[position];
170174
byte childDepth = depth;
171175
childDepth++;
172-
property.ToString(result, true, indent, cr, lf, childDepth);
176+
result.Append('\"');
177+
result.Append(property.Name);
178+
result.Append('\"');
179+
result.Append(':');
180+
property.ToString(result, settings, childDepth);
173181
position++;
174182

175183
if (position == Count)
@@ -178,39 +186,21 @@ public readonly void ToString(Text result, ReadOnlySpan<char> indent = default,
178186
}
179187

180188
result.Append(',');
181-
NewLine();
189+
settings.NewLine(result);
182190
for (int i = 0; i <= depth; i++)
183191
{
184-
Indent(indent);
192+
settings.Indent(result);
185193
}
186194
}
187195

188-
NewLine();
196+
settings.NewLine(result);
189197
for (int i = 0; i < depth; i++)
190198
{
191-
Indent(indent);
199+
settings.Indent(result);
192200
}
193201
}
194202

195203
result.Append('}');
196-
197-
void NewLine()
198-
{
199-
if (cr)
200-
{
201-
result.Append('\r');
202-
}
203-
204-
if (lf)
205-
{
206-
result.Append('\n');
207-
}
208-
}
209-
210-
void Indent(ReadOnlySpan<char> indent)
211-
{
212-
result.Append(indent);
213-
}
214204
}
215205

216206
public readonly override string ToString()

source/JSON/JSONProperty.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,14 @@ public void Dispose()
266266
type = default;
267267
}
268268

269-
public readonly void ToString(Text result, bool prefixName, ReadOnlySpan<char> indent = default, bool cr = false, bool lf = false, byte depth = 0)
269+
public readonly void ToString(Text result, SerializationSettings settings = default)
270270
{
271-
ThrowIfDisposed();
271+
ToString(result, settings, 0);
272+
}
272273

273-
if (prefixName)
274-
{
275-
result.Append('\"');
276-
result.Append(Name);
277-
result.Append('\"');
278-
result.Append(':');
279-
}
274+
internal readonly void ToString(Text result, SerializationSettings settings, byte depth)
275+
{
276+
ThrowIfDisposed();
280277

281278
if (type == Type.Text)
282279
{
@@ -298,12 +295,12 @@ public readonly void ToString(Text result, bool prefixName, ReadOnlySpan<char> i
298295
else if (type == Type.Object)
299296
{
300297
JSONObject jsonObject = value.Read<JSONObject>();
301-
jsonObject.ToString(result, indent, cr, lf, depth);
298+
jsonObject.ToString(result, settings, depth);
302299
}
303300
else if (type == Type.Array)
304301
{
305302
JSONArray jsonArray = value.Read<JSONArray>();
306-
jsonArray.ToString(result, indent, cr, lf, depth);
303+
jsonArray.ToString(result, settings, depth);
307304
}
308305
else if (type == Type.Null)
309306
{

source/JSON/JSONReader.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public JSONReader(ByteReader reader)
3030
this.reader = reader;
3131
}
3232

33+
public readonly bool PeekToken(out Token token)
34+
{
35+
return PeekToken(out token, out _);
36+
}
37+
3338
public readonly bool PeekToken(out Token token, out int readBytes)
3439
{
3540
token = default;
@@ -193,11 +198,11 @@ public readonly bool ReadBoolean()
193198
else if (token.type == Token.Type.Text)
194199
{
195200
int length = GetText(token, buffer);
196-
if (buffer.Slice(0, length).SequenceEqual("true"))
201+
if (buffer.Slice(0, length).SequenceEqual(Token.True))
197202
{
198203
return true;
199204
}
200-
else if (buffer.Slice(0, length).SequenceEqual("false"))
205+
else if (buffer.Slice(0, length).SequenceEqual(Token.False))
201206
{
202207
return false;
203208
}
@@ -242,7 +247,7 @@ public readonly T ReadObject<T>() where T : unmanaged, IJSONSerializable
242247
throw new InvalidOperationException("Expected start object token.");
243248
}
244249

245-
public unsafe readonly int GetText(Token token, Span<char> destination)
250+
public readonly int GetText(Token token, Span<char> destination)
246251
{
247252
return reader.PeekUTF8(token.position, token.length, destination);
248253
}
@@ -258,7 +263,7 @@ public readonly bool GetBoolean(Token token)
258263
{
259264
Span<char> buffer = stackalloc char[token.length];
260265
int length = GetText(token, buffer);
261-
return buffer.Slice(0, length).SequenceEqual("true".AsSpan());
266+
return buffer.Slice(0, length).SequenceEqual(Token.True);
262267
}
263268
}
264269
}

0 commit comments

Comments
 (0)