This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use the json.net parser instead of manually parsing the json - Simplified some test code
- Loading branch information
Victor Hurdugaci
committed
Apr 28, 2015
1 parent
7d42e66
commit 0517eee
Showing
24 changed files
with
770 additions
and
331 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/Microsoft.Framework.ConfigurationModel.Json/JsonConfigurationFileParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Framework.ConfigurationModel.Json | ||
{ | ||
internal class JsonConfigurationFileParser | ||
{ | ||
private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
private readonly Stack<string> _context = new Stack<string>(); | ||
private string _currentPath; | ||
|
||
private JsonTextReader _reader; | ||
|
||
public IDictionary<string, string> Parse(Stream input) | ||
{ | ||
_data.Clear(); | ||
_reader = new JsonTextReader(new StreamReader(input)); | ||
_reader.DateParseHandling = DateParseHandling.None; | ||
|
||
var jsonConfig = JObject.Load(_reader); | ||
|
||
VisitJObject(jsonConfig); | ||
|
||
return _data; | ||
} | ||
|
||
private void VisitJObject(JObject jObject) | ||
{ | ||
foreach (var property in jObject.Properties()) | ||
{ | ||
EnterContext(property.Name); | ||
VisitProperty(property); | ||
ExitContext(); | ||
} | ||
} | ||
|
||
private void VisitProperty(JProperty property) | ||
{ | ||
VisitToken(property.Value); | ||
} | ||
|
||
private void VisitToken(JToken token) | ||
{ | ||
switch (token.Type) | ||
{ | ||
case JTokenType.Object: | ||
VisitJObject(token.Value<JObject>()); | ||
break; | ||
|
||
case JTokenType.Array: | ||
VisitArray(token.Value<JArray>()); | ||
break; | ||
|
||
case JTokenType.Integer: | ||
case JTokenType.Float: | ||
case JTokenType.String: | ||
case JTokenType.Boolean: | ||
case JTokenType.Bytes: | ||
case JTokenType.Raw: | ||
case JTokenType.Null: | ||
VisitPrimitive(token); | ||
break; | ||
|
||
default: | ||
throw new FormatException(Resources.FormatError_UnsupportedJSONToken( | ||
_reader.TokenType, | ||
_reader.Path, | ||
_reader.LineNumber, | ||
_reader.LinePosition)); | ||
} | ||
} | ||
|
||
private void VisitArray(JArray array) | ||
{ | ||
for (int index = 0; index < array.Count; index++) | ||
{ | ||
EnterContext(index.ToString()); | ||
VisitToken(array[index]); | ||
ExitContext(); | ||
} | ||
} | ||
|
||
private void VisitPrimitive(JToken data) | ||
{ | ||
var key = _currentPath; | ||
|
||
if (_data.ContainsKey(key)) | ||
{ | ||
throw new FormatException(Resources.FormatError_KeyIsDuplicated(key)); | ||
} | ||
_data[key] = data.ToString(); | ||
} | ||
|
||
private void EnterContext(string context) | ||
{ | ||
_context.Push(context); | ||
_currentPath = string.Join(":", _context.Reverse()); | ||
} | ||
|
||
private void ExitContext() | ||
{ | ||
_context.Pop(); | ||
_currentPath = string.Join(":", _context.Reverse()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/Microsoft.Framework.ConfigurationModel.Json/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Microsoft.Framework.ConfigurationModel/ConfigurationKeyComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Framework.ConfigurationModel | ||
{ | ||
public class ConfigurationKeyComparer : IComparer<string> | ||
{ | ||
private const char Separator = ':'; | ||
|
||
public static ConfigurationKeyComparer Instance { get; } = new ConfigurationKeyComparer(); | ||
|
||
public int Compare(string x, string y) | ||
{ | ||
var xParts = x?.Split(Separator) ?? new string[0]; | ||
var yParts = y?.Split(Separator) ?? new string[0]; | ||
|
||
// Compare each part until we get two parts that are not equal | ||
for (int i = 0; i < Math.Min(xParts.Length, yParts.Length); i++) | ||
{ | ||
x = xParts[i]; | ||
y = yParts[i]; | ||
|
||
var value1 = 0; | ||
var value2 = 0; | ||
|
||
var xIsInt = x != null && int.TryParse(x, out value1); | ||
var yIsInt = y != null && int.TryParse(y, out value2); | ||
|
||
int result = 0; | ||
|
||
if (!xIsInt && !yIsInt) | ||
{ | ||
// Both are strings | ||
result = string.Compare(x, y, StringComparison.OrdinalIgnoreCase); | ||
} | ||
else if (xIsInt && yIsInt) | ||
{ | ||
// Both are int | ||
result = value1 - value2; | ||
} | ||
else | ||
{ | ||
// Only one of them is int | ||
result = xIsInt ? -1 : 1; | ||
} | ||
|
||
if (result != 0) | ||
{ | ||
// One of them is different | ||
return result; | ||
} | ||
} | ||
|
||
// If we get here, the common parts are equal. | ||
// If they are of the same length, then they are totally identical | ||
return xParts.Length - yParts.Length; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.