Skip to content

Commit b062426

Browse files
author
Jack Brookes
committed
fix json indentation
1 parent 2908d12 commit b062426

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

Assets/UXF/Scripts/Etc/MiniJSON.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public static string Serialize(object obj)
470470
{
471471
var instance = new Serializer();
472472

473-
int level = 1;
473+
int level = 0;
474474
instance.SerializeValue(obj, ref level);
475475

476476
return instance.builder.ToString();
@@ -517,13 +517,13 @@ void SerializeObject(IDictionary obj, ref int level)
517517
level++;
518518
bool first = true;
519519

520-
builder.Append("{\n" + Spaces(level));
520+
builder.Append("{\r\n" + Spaces(level));
521521

522522
foreach (object e in obj.Keys)
523523
{
524524
if (!first)
525525
{
526-
builder.Append(",\n" + Spaces(level));
526+
builder.Append(",\r\n" + Spaces(level));
527527
}
528528

529529
SerializeString(e.ToString());
@@ -535,21 +535,21 @@ void SerializeObject(IDictionary obj, ref int level)
535535
}
536536

537537
level--;
538-
builder.Append("\n" + Spaces(level) + "}");
538+
builder.Append("\r\n" + Spaces(level) + "}");
539539
}
540540

541541
void SerializeArray(IList anArray, ref int level)
542542
{
543543
level++;
544-
builder.Append("[\n" + Spaces(level));
544+
builder.Append("[\r\n" + Spaces(level));
545545

546546
bool first = true;
547547

548548
foreach (object obj in anArray)
549549
{
550550
if (!first)
551551
{
552-
builder.Append(",\n" + Spaces(level));
552+
builder.Append(",\r\n" + Spaces(level));
553553
}
554554

555555
SerializeValue(obj, ref level);
@@ -559,7 +559,7 @@ void SerializeArray(IList anArray, ref int level)
559559

560560

561561
level--;
562-
builder.Append("\n" + Spaces(level) + "]");
562+
builder.Append("\r\n" + Spaces(level) + "]");
563563

564564
}
565565

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
9+
namespace UXF.Tests
10+
{
11+
public class TestJSON
12+
{
13+
14+
Dictionary<string, object> dict = new Dictionary<string, object>()
15+
{
16+
{"string", "aaa"},
17+
{"bool", true},
18+
{"int", 3},
19+
{"float", 3.14f},
20+
{"array", new List<object>(){1, 2, 3, 4}},
21+
{"object", new Dictionary<string, object>(){{"a", 1}, {"b", "abc"}}}
22+
};
23+
24+
const string expectedOutput = @"{
25+
""string"": ""aaa"",
26+
""bool"": true,
27+
""int"": 3,
28+
""float"": ""3.14"",
29+
""array"": [
30+
1,
31+
2,
32+
3,
33+
4
34+
],
35+
""object"": {
36+
""a"": 1,
37+
""b"": ""abc""
38+
}
39+
}";
40+
41+
[Test]
42+
public void JSONIndentation()
43+
{
44+
string outString = MiniJSON.Json.Serialize(dict);
45+
Assert.AreEqual(expectedOutput, outString);
46+
Debug.Log(expectedOutput);
47+
}
48+
}
49+
50+
}

Assets/UXF/Scripts/Tests/Editor/TestJSON.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.

0 commit comments

Comments
 (0)