Skip to content

Commit b127141

Browse files
committed
Fix escaping of backslash characters (do not modify them);
Refactor VectorTemplates to use extension methods; Add ToJson extension method for all vector template methods; Add tests for unicode characters; Refactor tests to share strings; Misc cleanup
1 parent 57a9410 commit b127141

9 files changed

+335
-85
lines changed

Editor/JSONChecker.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ THE SOFTWARE.
2222

2323
//#define JSONOBJECT_PERFORMANCE_TEST //For testing performance of parse/stringify. Turn on editor profiling to see how we're doing
2424

25+
using Defective.JSON.Tests;
2526
using UnityEngine;
2627
using UnityEditor;
2728
#if UNITY_2017_1_OR_NEWER
@@ -34,7 +35,7 @@ THE SOFTWARE.
3435

3536
namespace Defective.JSON {
3637
public class JSONChecker : EditorWindow {
37-
string testJsonString = "{\r\n\t\"TestObject\":{\r\n\t\t\"SomeText\":\"Blah\",\r\n\t\t\"SomeObject\":{\r\n\t\t\t\"SomeNumber\":42,\r\n\t\t\t\"SomeFloat\":13.37,\r\n\t\t\t\"SomeBool\":true,\r\n\t\t\t\"SomeNull\":null\r\n\t\t},\r\n\t\t\"SomeEmptyObject\":{},\r\n\t\t\"SomeEmptyArray\":[],\r\n\t\t\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"\r\n\t}\r\n}";
38+
string testJsonString = JSONObjectTestStrings.PrettyJsonString;
3839
string url = "";
3940
JSONObject jsonObject;
4041

Editor/JSONObjectAsyncTests.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ THE SOFTWARE.
2424

2525
#if UNITY_5_6_OR_NEWER && JSONOBJECT_TESTS
2626
using NUnit.Framework;
27+
using TestStrings = Defective.JSON.Tests.JSONObjectTestStrings;
2728

2829
namespace Defective.JSON.Tests {
2930
class JSONObjectAsyncTests {
30-
const string TestJsonString = "{\"TestObject\":{\"SomeText\":\"Blah\",\"SomeObject\":{\"SomeNumber\":42,\"SomeFloat\":13.37,\"SomeBool\":true,\"SomeNull\":null},\"SomeEmptyObject\":{},\"SomeEmptyArray\":[],\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"}}";
31-
const string TestPrettyJsonString = "{\r\n\t\"TestObject\":{\r\n\t\t\"SomeText\":\"Blah\",\r\n\t\t\"SomeObject\":{\r\n\t\t\t\"SomeNumber\":42,\r\n\t\t\t\"SomeFloat\":13.37,\r\n\t\t\t\"SomeBool\":true,\r\n\t\t\t\"SomeNull\":null\r\n\t\t},\r\n\t\t\"SomeEmptyObject\":{},\r\n\t\t\"SomeEmptyArray\":[],\r\n\t\t\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"\r\n\t}\r\n}";
32-
const string TestFieldName = "TestField";
33-
const string TestJsonFormat = "{{\"" + TestFieldName + "\":{0}}}";
34-
const string TestJsonFormatFloat = "{{\"" + TestFieldName + "\":{0:R}}}";
35-
3631
static void ValidateJsonString(JSONObject jsonObject, string expected, bool pretty = false) {
3732
using (var enumerator = jsonObject.PrintAsync(pretty).GetEnumerator()) {
3833
while (enumerator.MoveNext()) { }
@@ -42,12 +37,12 @@ static void ValidateJsonString(JSONObject jsonObject, string expected, bool pret
4237

4338
[Test]
4439
public void InputMatchesOutput() {
45-
ValidateJsonString(new JSONObject(TestJsonString), TestJsonString);
40+
ValidateJsonString(new JSONObject(TestStrings.JsonString), TestStrings.JsonString);
4641
}
4742

4843
[Test]
4944
public void PrettyInputMatchesPrettyOutput() {
50-
ValidateJsonString(new JSONObject(TestPrettyJsonString), TestPrettyJsonString, true);
45+
ValidateJsonString(new JSONObject(TestStrings.PrettyJsonString), TestStrings.PrettyJsonString, true);
5146
}
5247

5348
[TestCase(long.MaxValue)]
@@ -56,8 +51,8 @@ public void PrettyInputMatchesPrettyOutput() {
5651
[TestCase(42)]
5752
public void EncodeLong(long value) {
5853
var jsonObject = new JSONObject();
59-
jsonObject.AddField(TestFieldName, value);
60-
ValidateJsonString(jsonObject, string.Format(TestJsonFormat, value));
54+
jsonObject.AddField(TestStrings.FieldName, value);
55+
ValidateJsonString(jsonObject, string.Format(TestStrings.JsonFormat, value));
6156
}
6257

6358
[TestCase(float.NegativeInfinity)]
@@ -69,15 +64,15 @@ public void EncodeLong(long value) {
6964
[TestCase(42)]
7065
public void EncodeFloat(float value) {
7166
var jsonObject = new JSONObject();
72-
jsonObject.AddField(TestFieldName, value);
67+
jsonObject.AddField(TestStrings.FieldName, value);
7368

7469
#if JSONOBJECT_USE_FLOAT
7570
var expected = value;
7671
#else
7772
var expected = (double) value;
7873
#endif
7974

80-
ValidateJsonString(jsonObject, string.Format(TestJsonFormatFloat, expected));
75+
ValidateJsonString(jsonObject, string.Format(TestStrings.JsonFormatFloat, expected));
8176
}
8277

8378
[TestCase(double.NegativeInfinity)]
@@ -91,23 +86,23 @@ public void EncodeFloat(float value) {
9186
[TestCase(42)]
9287
public void EncodeDouble(double value) {
9388
var jsonObject = new JSONObject();
94-
jsonObject.AddField(TestFieldName, value);
89+
jsonObject.AddField(TestStrings.FieldName, value);
9590

9691
#if JSONOBJECT_USE_FLOAT
9792
var expected = (float) value;
9893
#else
9994
var expected = value;
10095
#endif
10196

102-
ValidateJsonString(jsonObject, string.Format(TestJsonFormatFloat, expected));
97+
ValidateJsonString(jsonObject, string.Format(TestStrings.JsonFormatFloat, expected));
10398
}
10499

105100
[TestCase(long.MaxValue)]
106101
[TestCase(long.MinValue)]
107102
[TestCase(0)]
108103
[TestCase(42)]
109104
public void EncodeAndParseLong(long value) {
110-
var jsonText = string.Format(TestJsonFormat, value);
105+
var jsonText = string.Format(TestStrings.JsonFormat, value);
111106
ValidateJsonString(new JSONObject(jsonText), jsonText);
112107
}
113108

@@ -119,7 +114,7 @@ public void EncodeAndParseLong(long value) {
119114
[TestCase(0)]
120115
[TestCase(42)]
121116
public void EncodeAndParseFloat(float value) {
122-
var jsonText = string.Format(TestJsonFormatFloat, value);
117+
var jsonText = string.Format(TestStrings.JsonFormatFloat, value);
123118
ValidateJsonString(new JSONObject(jsonText), jsonText);
124119
}
125120

@@ -133,7 +128,7 @@ public void EncodeAndParseFloat(float value) {
133128
[TestCase(0)]
134129
[TestCase(42)]
135130
public void EncodeAndParseDouble(double value) {
136-
var jsonText = string.Format(TestJsonFormatFloat, value);
131+
var jsonText = string.Format(TestStrings.JsonFormatFloat, value);
137132

138133
#if JSONOBJECT_USE_FLOAT
139134
var expected = string.Format(TestJsonFormatFloat, (float) value);
@@ -143,6 +138,17 @@ public void EncodeAndParseDouble(double value) {
143138

144139
ValidateJsonString(new JSONObject(jsonText), expected);
145140
}
141+
142+
[TestCase("Hello World!")]
143+
[TestCase("")]
144+
[TestCase("æ")]
145+
[TestCase("ø")]
146+
[TestCase("\\u00e6")]
147+
[TestCase("\\u00f8")]
148+
public void EncodeAndParseString(string value) {
149+
var jsonText = string.Format(TestStrings.JsonFormatString, value);
150+
ValidateJsonString(new JSONObject(jsonText), jsonText);
151+
}
146152
}
147153
}
148154
#endif

Editor/JSONObjectTestStrings.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright (c) 2010-2021 Matt Schoen
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
#if UNITY_5_6_OR_NEWER && JSONOBJECT_TESTS
24+
25+
namespace Defective.JSON.Tests {
26+
static class JSONObjectTestStrings {
27+
public const string JsonString = "{\"TestObject\":{\"SomeText\":\"Blah\",\"SomeObject\":{\"SomeNumber\":42,\"SomeFloat\":13.37,\"SomeBool\":true,\"SomeNull\":null},\"SomeEmptyObject\":{},\"SomeEmptyArray\":[],\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"}}";
28+
public const string PrettyJsonString = "{\r\n\t\"TestObject\":{\r\n\t\t\"SomeText\":\"Blah\",\r\n\t\t\"SomeObject\":{\r\n\t\t\t\"SomeNumber\":42,\r\n\t\t\t\"SomeFloat\":13.37,\r\n\t\t\t\"SomeBool\":true,\r\n\t\t\t\"SomeNull\":null\r\n\t\t},\r\n\t\t\"SomeEmptyObject\":{},\r\n\t\t\"SomeEmptyArray\":[],\r\n\t\t\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"\r\n\t}\r\n}";
29+
public const string FieldName = "TestField";
30+
public const string JsonFormat = "{{\"" + FieldName + "\":{0}}}";
31+
public const string JsonFormatString = "{{\"" + FieldName + "\":\"{0}\"}}";
32+
public const string JsonFormatFloat = "{{\"" + FieldName + "\":{0:R}}}";
33+
}
34+
}
35+
#endif

Editor/JSONObjectTestStrings.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/JSONObjectTests.cs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,32 @@ THE SOFTWARE.
2424

2525
#if UNITY_5_6_OR_NEWER && JSONOBJECT_TESTS
2626
using NUnit.Framework;
27+
using UnityEngine;
28+
using TestStrings = Defective.JSON.Tests.JSONObjectTestStrings;
2729

2830
namespace Defective.JSON.Tests {
2931
class JSONObjectTests {
30-
const string TestJsonString = "{\"TestObject\":{\"SomeText\":\"Blah\",\"SomeObject\":{\"SomeNumber\":42,\"SomeFloat\":13.37,\"SomeBool\":true,\"SomeNull\":null},\"SomeEmptyObject\":{},\"SomeEmptyArray\":[],\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"}}";
31-
const string TestPrettyJsonString = "{\r\n\t\"TestObject\":{\r\n\t\t\"SomeText\":\"Blah\",\r\n\t\t\"SomeObject\":{\r\n\t\t\t\"SomeNumber\":42,\r\n\t\t\t\"SomeFloat\":13.37,\r\n\t\t\t\"SomeBool\":true,\r\n\t\t\t\"SomeNull\":null\r\n\t\t},\r\n\t\t\"SomeEmptyObject\":{},\r\n\t\t\"SomeEmptyArray\":[],\r\n\t\t\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"\r\n\t}\r\n}";
32-
const string TestFieldName = "TestField";
33-
const string TestJsonFormat = "{{\"" + TestFieldName + "\":{0}}}";
34-
const string TestJsonFormatFloat = "{{\"" + TestFieldName + "\":{0:R}}}";
35-
3632
static void ValidateJsonString(JSONObject jsonObject, string expected, bool pretty = false) {
3733
Assert.That(jsonObject.ToString(pretty), Is.EqualTo(expected));
3834
}
3935

4036
[Test]
4137
public void InputMatchesOutput() {
42-
ValidateJsonString(new JSONObject(TestJsonString), TestJsonString);
38+
ValidateJsonString(new JSONObject(TestStrings.JsonString), TestStrings.JsonString);
4339
}
4440

4541
[Test]
4642
public void PrettyInputMatchesPrettyOutput() {
47-
ValidateJsonString(new JSONObject(TestPrettyJsonString), TestPrettyJsonString, true);
43+
ValidateJsonString(new JSONObject(TestStrings.PrettyJsonString), TestStrings.PrettyJsonString, true);
4844
}
4945

5046
[TestCase(long.MaxValue)]
5147
[TestCase(long.MinValue)]
5248
[TestCase(0)]
5349
[TestCase(42)]
5450
public void ParseLong(long value) {
55-
var jsonObject = new JSONObject(string.Format(TestJsonFormat, value));
56-
Assert.That(jsonObject[TestFieldName].longValue, Is.EqualTo(value));
51+
var jsonObject = new JSONObject(string.Format(TestStrings.JsonFormat, value));
52+
Assert.That(jsonObject[TestStrings.FieldName].longValue, Is.EqualTo(value));
5753
}
5854

5955
[TestCase(float.NegativeInfinity)]
@@ -64,8 +60,8 @@ public void ParseLong(long value) {
6460
[TestCase(0)]
6561
[TestCase(42)]
6662
public void ParseFloat(float value) {
67-
var jsonObject = new JSONObject(string.Format(TestJsonFormatFloat, value));
68-
Assert.That(jsonObject[TestFieldName].floatValue, Is.EqualTo(value));
63+
var jsonObject = new JSONObject(string.Format(TestStrings.JsonFormatFloat, value));
64+
Assert.That(jsonObject[TestStrings.FieldName].floatValue, Is.EqualTo(value));
6965
}
7066

7167
[TestCase(double.NegativeInfinity)]
@@ -78,12 +74,12 @@ public void ParseFloat(float value) {
7874
[TestCase(0)]
7975
[TestCase(42)]
8076
public void ParseDouble(double value) {
81-
var jsonObject = new JSONObject(string.Format(TestJsonFormatFloat, value));
77+
var jsonObject = new JSONObject(string.Format(TestStrings.JsonFormatFloat, value));
8278

8379
#if JSONOBJECT_USE_FLOAT
84-
Assert.That(jsonObject[TestFieldName].floatValue, Is.EqualTo((float) value));
80+
Assert.That(jsonObject[TestStrings.TestFieldName].floatValue, Is.EqualTo((float) value));
8581
#else
86-
Assert.That(jsonObject[TestFieldName].doubleValue, Is.EqualTo(value));
82+
Assert.That(jsonObject[TestStrings.FieldName].doubleValue, Is.EqualTo(value));
8783
#endif
8884
}
8985

@@ -93,8 +89,8 @@ public void ParseDouble(double value) {
9389
[TestCase(42)]
9490
public void EncodeLong(long value) {
9591
var jsonObject = new JSONObject();
96-
jsonObject.AddField(TestFieldName, value);
97-
ValidateJsonString(jsonObject, string.Format(TestJsonFormat, value));
92+
jsonObject.AddField(TestStrings.FieldName, value);
93+
ValidateJsonString(jsonObject, string.Format(TestStrings.JsonFormat, value));
9894
}
9995

10096
[TestCase(float.NegativeInfinity)]
@@ -106,15 +102,15 @@ public void EncodeLong(long value) {
106102
[TestCase(42)]
107103
public void EncodeFloat(float value) {
108104
var jsonObject = new JSONObject();
109-
jsonObject.AddField(TestFieldName, value);
105+
jsonObject.AddField(TestStrings.FieldName, value);
110106

111107
#if JSONOBJECT_USE_FLOAT
112108
var expected = value;
113109
#else
114110
var expected = (double) value;
115111
#endif
116112

117-
ValidateJsonString(jsonObject, string.Format(TestJsonFormatFloat, expected));
113+
ValidateJsonString(jsonObject, string.Format(TestStrings.JsonFormatFloat, expected));
118114
}
119115

120116
[TestCase(double.NegativeInfinity)]
@@ -128,23 +124,23 @@ public void EncodeFloat(float value) {
128124
[TestCase(42)]
129125
public void EncodeDouble(double value) {
130126
var jsonObject = new JSONObject();
131-
jsonObject.AddField(TestFieldName, value);
127+
jsonObject.AddField(TestStrings.FieldName, value);
132128

133129
#if JSONOBJECT_USE_FLOAT
134130
var expected = (float) value;
135131
#else
136132
var expected = value;
137133
#endif
138134

139-
ValidateJsonString(jsonObject, string.Format(TestJsonFormatFloat, expected));
135+
ValidateJsonString(jsonObject, string.Format(TestStrings.JsonFormatFloat, expected));
140136
}
141137

142138
[TestCase(long.MaxValue)]
143139
[TestCase(long.MinValue)]
144140
[TestCase(0)]
145141
[TestCase(42)]
146142
public void EncodeAndParseLong(long value) {
147-
var jsonText = string.Format(TestJsonFormat, value);
143+
var jsonText = string.Format(TestStrings.JsonFormat, value);
148144
ValidateJsonString(new JSONObject(jsonText), jsonText);
149145
}
150146

@@ -156,7 +152,7 @@ public void EncodeAndParseLong(long value) {
156152
[TestCase(0)]
157153
[TestCase(42)]
158154
public void EncodeAndParseFloat(float value) {
159-
var jsonText = string.Format(TestJsonFormatFloat, value);
155+
var jsonText = string.Format(TestStrings.JsonFormatFloat, value);
160156
ValidateJsonString(new JSONObject(jsonText), jsonText);
161157
}
162158

@@ -170,7 +166,7 @@ public void EncodeAndParseFloat(float value) {
170166
[TestCase(0)]
171167
[TestCase(42)]
172168
public void EncodeAndParseDouble(double value) {
173-
var jsonText = string.Format(TestJsonFormatFloat, value);
169+
var jsonText = string.Format(TestStrings.JsonFormatFloat, value);
174170

175171
#if JSONOBJECT_USE_FLOAT
176172
var expected = string.Format(TestJsonFormatFloat, (float) value);
@@ -180,6 +176,17 @@ public void EncodeAndParseDouble(double value) {
180176

181177
ValidateJsonString(new JSONObject(jsonText), expected);
182178
}
179+
180+
[TestCase("Hello World!")]
181+
[TestCase("")]
182+
[TestCase("æ")]
183+
[TestCase("ø")]
184+
[TestCase("\\u00e6")]
185+
[TestCase("\\u00f8")]
186+
public void EncodeAndParseString(string value) {
187+
var jsonText = string.Format(TestStrings.JsonFormatString, value);
188+
ValidateJsonString(new JSONObject(jsonText), jsonText);
189+
}
183190
}
184191
}
185192
#endif

0 commit comments

Comments
 (0)