Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions JsonViewer/Model/DeserializeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@

public class DeserializeResult
{
private Dictionary<string, object> _dictionary;
private SortedDictionary<string, object> _dictionary;
private string _preJsonText = string.Empty;
private string _postJsonText = string.Empty;

public DeserializeResult(Dictionary<string, object> dictionary)
{
_dictionary = dictionary;
_dictionary = new SortedDictionary<string, object>(dictionary, new DictionaryComparer());
}

public DeserializeResult(Dictionary<string, object> dictionary, string preJsonText, string postJsonText)
{
_dictionary = dictionary;
_dictionary = new SortedDictionary<string, object>(dictionary, new DictionaryComparer());
_preJsonText = preJsonText;
_postJsonText = postJsonText;
}

public Dictionary<string, object> Dictionary { get => _dictionary; }
public SortedDictionary<string, object> Dictionary { get => _dictionary; }

public string PreJsonText { get => _preJsonText; }

public string PostJsonText { get => _postJsonText; }

public bool HasExtraText { get => !string.IsNullOrEmpty(this.PreJsonText) || !string.IsNullOrEmpty(this.PostJsonText); }

public Dictionary<string, object> GetEverythingDictionary()
public SortedDictionary<string, object> GetEverythingDictionary()
{
Dictionary<string, object> dict = this.Dictionary;
SortedDictionary<string, object> dict = this.Dictionary;
if (this.HasExtraText)
{
dict = new Dictionary<string, object>();
dict = new SortedDictionary<string, object>(new DictionaryComparer());
if (!string.IsNullOrEmpty(this.PreJsonText))
{
dict["Pre-JSON text"] = this.PreJsonText;
dict[DictionaryComparer.PreJsonKey] = this.PreJsonText;
}

foreach (string key in this.Dictionary.Keys)
Expand All @@ -46,19 +46,47 @@ public Dictionary<string, object> GetEverythingDictionary()

if (!string.IsNullOrEmpty(this.PostJsonText))
{
dict["Post-JSON text"] = this.PostJsonText;
dict[DictionaryComparer.PostJsonKey] = this.PostJsonText;
}
}

return dict;
}
}

#pragma warning disable SA1402 // File may only contain a single class
#pragma warning disable SA1204 // Static elements must appear before instance elements
public static class DeserializeResultExtensions
#pragma warning restore SA1204 // Static elements must appear before instance elements
#pragma warning restore SA1402 // File may only contain a single class
internal class DictionaryComparer : IComparer<string>
{
public const string PreJsonKey = "Pre-JSON text";
public const string PostJsonKey = "Post-JSON text";

public int Compare(string x, string y)
{
if (x == y)
{
return 0;
}

switch (x)
{
case PreJsonKey:
return -1;
case PostJsonKey:
return 1;
}

switch (y)
{
case PreJsonKey:
return 1;
case PostJsonKey:
return -1;
}

return x.CompareTo(y);
}
}

internal static class DeserializeResultExtensions
{
public static bool IsSuccessful(this DeserializeResult deserializeResult)
{
Expand Down
2 changes: 1 addition & 1 deletion JsonViewer/Model/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public async Task TreatAsJson()
DeserializeResult deserializeResult = await JsonObjectFactory.TryDeserialize(this.Value as string);
FileLogger.Assert(deserializeResult != null);

Dictionary<string, object> dict = deserializeResult.GetEverythingDictionary();
SortedDictionary<string, object> dict = deserializeResult.GetEverythingDictionary();
this.Value = dict;
this.EnsureValues();
FileLogger.Assert(_dataType == DataType.Json);
Expand Down
18 changes: 11 additions & 7 deletions JsonViewer/Model/JsonObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static Task<DeserializeResult> TrySimpleDeserialize(string jsonString)
});
}

public static void Flatten(ref List<JsonObject> items, Dictionary<string, object> dictionary, JsonObject parent)
public static void Flatten(ref List<JsonObject> items, SortedDictionary<string, object> dictionary, JsonObject parent)
{
List<JsonObject> children = new List<JsonObject>();
foreach (string key in dictionary.Keys)
Expand All @@ -213,13 +213,17 @@ public static void Flatten(ref List<JsonObject> items, Dictionary<string, object

if (rawObject != null)
{
if (rawObject is Dictionary<string, object>)
if (rawObject is SortedDictionary<string, object> sortedDictionary)
{
Flatten(ref items, rawObject as Dictionary<string, object>, data);
Flatten(ref items, sortedDictionary, data);
}
else if (rawObject is System.Collections.ArrayList)
else if (rawObject is Dictionary<string, object> unsortedDictionary)
{
Flatten(ref items, rawObject as System.Collections.ArrayList, data);
Flatten(ref items, new SortedDictionary<string, object>(unsortedDictionary, new DictionaryComparer()), data);
}
else if (rawObject is System.Collections.ArrayList arrayList)
{
Flatten(ref items, arrayList, data);
}
}
}
Expand All @@ -242,9 +246,9 @@ public static void Flatten(ref List<JsonObject> items, System.Collections.ArrayL
items.Add(data);
}

if (rawObject is Dictionary<string, object>)
if (rawObject is SortedDictionary<string, object>)
{
Flatten(ref items, rawObject as Dictionary<string, object>, data);
Flatten(ref items, rawObject as SortedDictionary<string, object>, data);
}
else if (rawObject is System.Collections.ArrayList)
{
Expand Down
2 changes: 1 addition & 1 deletion JsonViewer/Model/RootObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public RootObject()
{
}

public static async Task<RootObject> Create(Dictionary<string, object> jsonObj)
public static async Task<RootObject> Create(SortedDictionary<string, object> jsonObj)
{
if (jsonObj == null)
{
Expand Down
4 changes: 2 additions & 2 deletions JsonViewer/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Task<bool> ReloadAsync()
});
}

public async Task<bool> ReloadAsync(Dictionary<string, object> dictionary)
public async Task<bool> ReloadAsync(SortedDictionary<string, object> dictionary)
{
RootObject rootObject = await RootObject.Create(dictionary);
if (rootObject == null)
Expand Down Expand Up @@ -177,7 +177,7 @@ public async Task<bool> SetText(string newText)
if (this._rawText != newText)
{
DeserializeResult deserializeResult = await JsonObjectFactory.TryAgressiveDeserialize(newText);
Dictionary<string, object> dictionary = deserializeResult?.GetEverythingDictionary();
SortedDictionary<string, object> dictionary = deserializeResult?.GetEverythingDictionary();
string newNormalizedText = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dictionary);
if (newNormalizedText != _lastText)
{
Expand Down