Merge pull request #2750 from eerhardt/RemoveJsonParser

Remove JsonParser dependency
This commit is contained in:
Eric Erhardt 2016-04-29 17:57:43 -05:00
commit 2a49edbb6e
3 changed files with 69 additions and 119 deletions

View file

@ -4,7 +4,6 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.JsonParser.Sources;
namespace Microsoft.DotNet.ProjectModel
{
@ -76,22 +75,6 @@ namespace Microsoft.DotNet.ProjectModel
return result;
}
internal static FileFormatException Create(string message, JsonValue jsonValue)
{
var result = new FileFormatException(message)
.WithLineInfo(jsonValue);
return result;
}
internal static FileFormatException Create(Exception exception, JsonValue jsonValue)
{
var result = new FileFormatException(exception.Message, exception)
.WithLineInfo(jsonValue);
return result;
}
internal FileFormatException WithFilePath(string path)
{
if (path == null)
@ -106,11 +89,6 @@ namespace Microsoft.DotNet.ProjectModel
private FileFormatException WithLineInfo(Exception exception)
{
if (exception is JsonDeserializerException)
{
WithLineInfo((JsonDeserializerException) exception);
}
if (exception is JsonReaderException)
{
WithLineInfo((JsonReaderException) exception);
@ -119,19 +97,6 @@ namespace Microsoft.DotNet.ProjectModel
return this;
}
private FileFormatException WithLineInfo(JsonDeserializerException exception)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
Line = exception.Line;
Column = exception.Column;
return this;
}
private FileFormatException WithLineInfo(JsonReaderException exception)
{
if (exception == null)
@ -145,19 +110,6 @@ namespace Microsoft.DotNet.ProjectModel
return this;
}
private FileFormatException WithLineInfo(JsonValue value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
Line = value.Line;
Column = value.Column;
return this;
}
private FileFormatException WithLineInfo(JToken value)
{
if (value == null)

View file

@ -6,7 +6,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.ProjectModel.Utilities;
using Microsoft.Extensions.JsonParser.Sources;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.Versioning;
@ -48,7 +49,7 @@ namespace Microsoft.DotNet.ProjectModel.Graph
try
{
var reader = new StreamReader(stream);
var jobject = JsonDeserializer.Deserialize(reader) as JsonObject;
var jobject = JObject.Load(new JsonTextReader(reader));
if (jobject == null)
{
@ -85,7 +86,7 @@ namespace Microsoft.DotNet.ProjectModel.Graph
{
try
{
var rootJObject = JsonDeserializer.Deserialize(new StreamReader(stream)) as JsonObject;
var rootJObject = JObject.ReadFrom(new JsonTextReader(new StreamReader(stream))) as JObject;
if (rootJObject == null)
{
@ -93,7 +94,7 @@ namespace Microsoft.DotNet.ProjectModel.Graph
}
var version = ReadInt(rootJObject, "version", defaultValue: int.MinValue);
var exports = ReadObject(rootJObject.ValueAsJsonObject("exports"), ReadTargetLibrary);
var exports = ReadObject(rootJObject.Value<JObject>("exports"), ReadTargetLibrary);
return new ExportFile(fragmentLockFilePath, version, exports);
@ -109,37 +110,38 @@ namespace Microsoft.DotNet.ProjectModel.Graph
}
}
private LockFile ReadLockFile(string lockFilePath, JsonObject cursor)
private LockFile ReadLockFile(string lockFilePath, JObject cursor)
{
var lockFile = new LockFile(lockFilePath);
lockFile.Version = ReadInt(cursor, "version", defaultValue: int.MinValue);
lockFile.Targets = ReadObject(cursor.ValueAsJsonObject("targets"), ReadTarget);
lockFile.ProjectFileDependencyGroups = ReadObject(cursor.ValueAsJsonObject("projectFileDependencyGroups"), ReadProjectFileDependencyGroup);
ReadLibrary(cursor.ValueAsJsonObject("libraries"), lockFile);
lockFile.Targets = ReadObject(cursor.Value<JObject>("targets"), ReadTarget);
lockFile.ProjectFileDependencyGroups = ReadObject(cursor.Value<JObject>("projectFileDependencyGroups"), ReadProjectFileDependencyGroup);
ReadLibrary(cursor.Value<JObject>("libraries"), lockFile);
return lockFile;
}
private void ReadLibrary(JsonObject json, LockFile lockFile)
private void ReadLibrary(JObject json, LockFile lockFile)
{
if (json == null)
{
return;
}
foreach (var key in json.Keys)
foreach (var child in json)
{
var value = json.ValueAsJsonObject(key);
var key = child.Key;
var value = json.Value<JObject>(key);
if (value == null)
{
throw FileFormatException.Create("The value type is not object.", json.Value(key));
throw FileFormatException.Create("The value type is not object.", json[key]);
}
var parts = key.Split(new[] { '/' }, 2);
var name = parts[0];
var version = parts.Length == 2 ? _symbols.GetVersion(parts[1]) : null;
var type = _symbols.GetString(value.ValueAsString("type")?.Value);
var type = _symbols.GetString(value.Value<string>("type"));
if (type == null || string.Equals(type, "package", StringComparison.OrdinalIgnoreCase))
{
@ -148,8 +150,8 @@ namespace Microsoft.DotNet.ProjectModel.Graph
Name = name,
Version = version,
IsServiceable = ReadBool(value, "serviceable", defaultValue: false),
Sha512 = ReadString(value.Value("sha512")),
Files = ReadPathArray(value.Value("files"), ReadString)
Sha512 = ReadString(value["sha512"]),
Files = ReadPathArray(value["files"], ReadString)
});
}
else if (type == "project")
@ -160,10 +162,10 @@ namespace Microsoft.DotNet.ProjectModel.Graph
Version = version
};
var pathValue = value.Value("path");
var pathValue = value["path"];
projectLibrary.Path = pathValue == null ? null : ReadString(pathValue);
var buildTimeDependencyValue = value.Value("msbuildProject");
var buildTimeDependencyValue = value["msbuildProject"];
projectLibrary.MSBuildProject = buildTimeDependencyValue == null ? null : ReadString(buildTimeDependencyValue);
lockFile.ProjectLibraries.Add(projectLibrary);
@ -171,9 +173,9 @@ namespace Microsoft.DotNet.ProjectModel.Graph
}
}
private LockFileTarget ReadTarget(string property, JsonValue json)
private LockFileTarget ReadTarget(string property, JToken json)
{
var jobject = json as JsonObject;
var jobject = json as JObject;
if (jobject == null)
{
throw FileFormatException.Create("The value type is not an object.", json);
@ -192,9 +194,9 @@ namespace Microsoft.DotNet.ProjectModel.Graph
return target;
}
private LockFileTargetLibrary ReadTargetLibrary(string property, JsonValue json)
private LockFileTargetLibrary ReadTargetLibrary(string property, JToken json)
{
var jobject = json as JsonObject;
var jobject = json as JObject;
if (jobject == null)
{
throw FileFormatException.Create("The value type is not an object.", json);
@ -209,28 +211,28 @@ namespace Microsoft.DotNet.ProjectModel.Graph
library.Version = _symbols.GetVersion(parts[1]);
}
library.Type = _symbols.GetString(jobject.ValueAsString("type"));
var framework = jobject.ValueAsString("framework");
library.Type = _symbols.GetString(jobject.Value<string>("type"));
var framework = jobject.Value<string>("framework");
if (framework != null)
{
library.TargetFramework = _symbols.GetFramework(framework);
}
library.Dependencies = ReadObject(jobject.ValueAsJsonObject("dependencies"), ReadPackageDependency);
library.FrameworkAssemblies = new HashSet<string>(ReadArray(jobject.Value("frameworkAssemblies"), ReadFrameworkAssemblyReference), StringComparer.OrdinalIgnoreCase);
library.RuntimeAssemblies = ReadObject(jobject.ValueAsJsonObject("runtime"), ReadFileItem);
library.CompileTimeAssemblies = ReadObject(jobject.ValueAsJsonObject("compile"), ReadFileItem);
library.ResourceAssemblies = ReadObject(jobject.ValueAsJsonObject("resource"), ReadFileItem);
library.NativeLibraries = ReadObject(jobject.ValueAsJsonObject("native"), ReadFileItem);
library.ContentFiles = ReadObject(jobject.ValueAsJsonObject("contentFiles"), ReadContentFile);
library.RuntimeTargets = ReadObject(jobject.ValueAsJsonObject("runtimeTargets"), ReadRuntimeTarget);
library.Dependencies = ReadObject(jobject.Value<JObject>("dependencies"), ReadPackageDependency);
library.FrameworkAssemblies = new HashSet<string>(ReadArray(jobject["frameworkAssemblies"], ReadFrameworkAssemblyReference), StringComparer.OrdinalIgnoreCase);
library.RuntimeAssemblies = ReadObject(jobject.Value<JObject>("runtime"), ReadFileItem);
library.CompileTimeAssemblies = ReadObject(jobject.Value<JObject>("compile"), ReadFileItem);
library.ResourceAssemblies = ReadObject(jobject.Value<JObject>("resource"), ReadFileItem);
library.NativeLibraries = ReadObject(jobject.Value<JObject>("native"), ReadFileItem);
library.ContentFiles = ReadObject(jobject.Value<JObject>("contentFiles"), ReadContentFile);
library.RuntimeTargets = ReadObject(jobject.Value<JObject>("runtimeTargets"), ReadRuntimeTarget);
return library;
}
private LockFileRuntimeTarget ReadRuntimeTarget(string property, JsonValue json)
private LockFileRuntimeTarget ReadRuntimeTarget(string property, JToken json)
{
var jsonObject = json as JsonObject;
var jsonObject = json as JObject;
if (jsonObject == null)
{
throw FileFormatException.Create("The value type is not an object.", json);
@ -238,48 +240,48 @@ namespace Microsoft.DotNet.ProjectModel.Graph
return new LockFileRuntimeTarget(
path: _symbols.GetString(property),
runtime: _symbols.GetString(jsonObject.ValueAsString("rid")),
assetType: _symbols.GetString(jsonObject.ValueAsString("assetType"))
runtime: _symbols.GetString(jsonObject.Value<string>("rid")),
assetType: _symbols.GetString(jsonObject.Value<string>("assetType"))
);
}
private LockFileContentFile ReadContentFile(string property, JsonValue json)
private LockFileContentFile ReadContentFile(string property, JToken json)
{
var contentFile = new LockFileContentFile()
{
Path = property
};
var jsonObject = json as JsonObject;
var jsonObject = json as JObject;
if (jsonObject != null)
{
BuildAction action;
BuildAction.TryParse(jsonObject.ValueAsString("buildAction"), out action);
BuildAction.TryParse(jsonObject.Value<string>("buildAction"), out action);
contentFile.BuildAction = action;
var codeLanguage = _symbols.GetString(jsonObject.ValueAsString("codeLanguage"));
var codeLanguage = _symbols.GetString(jsonObject.Value<string>("codeLanguage"));
if (codeLanguage == "any")
{
codeLanguage = null;
}
contentFile.CodeLanguage = codeLanguage;
contentFile.OutputPath = jsonObject.ValueAsString("outputPath");
contentFile.PPOutputPath = jsonObject.ValueAsString("ppOutputPath");
contentFile.OutputPath = jsonObject.Value<string>("outputPath");
contentFile.PPOutputPath = jsonObject.Value<string>("ppOutputPath");
contentFile.CopyToOutput = ReadBool(jsonObject, "copyToOutput", false);
}
return contentFile;
}
private ProjectFileDependencyGroup ReadProjectFileDependencyGroup(string property, JsonValue json)
private ProjectFileDependencyGroup ReadProjectFileDependencyGroup(string property, JToken json)
{
return new ProjectFileDependencyGroup(
string.IsNullOrEmpty(property) ? null : NuGetFramework.Parse(property),
ReadArray(json, ReadString));
}
private PackageDependency ReadPackageDependency(string property, JsonValue json)
private PackageDependency ReadPackageDependency(string property, JToken json)
{
var versionStr = ReadString(json);
return new PackageDependency(
@ -287,88 +289,88 @@ namespace Microsoft.DotNet.ProjectModel.Graph
versionStr == null ? null : _symbols.GetVersionRange(versionStr));
}
private LockFileItem ReadFileItem(string property, JsonValue json)
private LockFileItem ReadFileItem(string property, JToken json)
{
var item = new LockFileItem { Path = _symbols.GetString(PathUtility.GetPathWithDirectorySeparator(property)) };
var jobject = json as JsonObject;
var jobject = json as JObject;
if (jobject != null)
{
foreach (var subProperty in jobject.Keys)
foreach (var child in jobject)
{
item.Properties[_symbols.GetString(subProperty)] = jobject.ValueAsString(subProperty);
item.Properties[_symbols.GetString(child.Key)] = jobject.Value<string>(child.Key);
}
}
return item;
}
private string ReadFrameworkAssemblyReference(JsonValue json)
private string ReadFrameworkAssemblyReference(JToken json)
{
return ReadString(json);
}
private static IList<TItem> ReadArray<TItem>(JsonValue json, Func<JsonValue, TItem> readItem)
private static IList<TItem> ReadArray<TItem>(JToken json, Func<JToken, TItem> readItem)
{
if (json == null)
{
return new List<TItem>();
}
var jarray = json as JsonArray;
var jarray = json as JArray;
if (jarray == null)
{
throw FileFormatException.Create("The value type is not array.", json);
}
var items = new List<TItem>();
for (int i = 0; i < jarray.Length; ++i)
for (int i = 0; i < jarray.Count; ++i)
{
items.Add(readItem(jarray[i]));
}
return items;
}
private IList<string> ReadPathArray(JsonValue json, Func<JsonValue, string> readItem)
private IList<string> ReadPathArray(JToken json, Func<JToken, string> readItem)
{
return ReadArray(json, readItem).Select(f => _symbols.GetString(PathUtility.GetPathWithDirectorySeparator(f))).ToList();
}
private static IList<TItem> ReadObject<TItem>(JsonObject json, Func<string, JsonValue, TItem> readItem)
private static IList<TItem> ReadObject<TItem>(JObject json, Func<string, JToken, TItem> readItem)
{
if (json == null)
{
return new List<TItem>();
}
var items = new List<TItem>();
foreach (var childKey in json.Keys)
foreach (var child in json)
{
items.Add(readItem(childKey, json.Value(childKey)));
items.Add(readItem(child.Key, json[child.Key]));
}
return items;
}
private static bool ReadBool(JsonObject cursor, string property, bool defaultValue)
private static bool ReadBool(JObject cursor, string property, bool defaultValue)
{
var valueToken = cursor.Value(property) as JsonBoolean;
if (valueToken == null)
var valueToken = cursor[property] as JValue;
if (valueToken == null || valueToken.Type != JTokenType.Boolean)
{
return defaultValue;
}
return valueToken.Value;
return (bool)valueToken.Value;
}
private static int ReadInt(JsonObject cursor, string property, int defaultValue)
private static int ReadInt(JObject cursor, string property, int defaultValue)
{
var number = cursor.Value(property) as JsonNumber;
if (number == null)
var number = cursor[property] as JValue;
if (number == null || number.Type != JTokenType.Integer)
{
return defaultValue;
}
try
{
var resultInInt = Convert.ToInt32(number.Raw);
var resultInInt = Convert.ToInt32(number.Value);
return resultInInt;
}
catch (Exception ex)
@ -378,13 +380,13 @@ namespace Microsoft.DotNet.ProjectModel.Graph
}
}
private string ReadString(JsonValue json)
private string ReadString(JToken json)
{
if (json is JsonString)
if (json.Type == JTokenType.String)
{
return _symbols.GetString((json as JsonString).Value);
return _symbols.GetString(json.ToString());
}
else if (json is JsonNull)
else if (json.Type == JTokenType.Null)
{
return null;
}

View file

@ -11,10 +11,6 @@
"type": "build",
"version": "1.0.0-rc2-20459"
},
"Microsoft.Extensions.JsonParser.Sources": {
"type": "build",
"version": "1.0.0-rc2-20221"
},
"Newtonsoft.Json": "7.0.1",
"NuGet.Packaging": "3.5.0-beta-1199",
"NuGet.RuntimeModel": "3.5.0-beta-1199",