Replace newtonsoft.json with ln.json

master
Harald Wolff 2019-08-19 14:12:26 +02:00
parent 6dc79e6682
commit 2ce2809530
8 changed files with 82 additions and 105 deletions

View File

@ -9,15 +9,13 @@
// **/ // **/
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using ln.types.odb; using ln.types.odb;
using Newtonsoft.Json;
using ln.http.exceptions; using ln.http.exceptions;
using Newtonsoft.Json.Linq;
using System.ComponentModel; using System.ComponentModel;
using ln.types.odb.mapped; using ln.types.odb.mapped;
using ln.types.threads; using ln.json.mapping;
using ln.json;
namespace ln.http.resources namespace ln.http.resources
{ {
public class CollectionResource<T> : Resource where T:class public class CollectionResource<T> : Resource where T:class
@ -83,7 +81,7 @@ namespace ln.http.resources
responseValue = instance; responseValue = instance;
break; break;
case "PUT": case "PUT":
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), instance); JSONMapper.DefaultMapper.Apply((JSONObject)JSONParser.Parse(httpRequest.ContentReader.ReadToEnd()), instance);
bool updated = collection.Update(instance); bool updated = collection.Update(instance);
responseValue = instance; responseValue = instance;
@ -95,7 +93,7 @@ namespace ln.http.resources
} }
httpResponse.ContentWriter.Write( httpResponse.ContentWriter.Write(
JsonConvert.SerializeObject(responseValue) JSONMapper.DefaultMapper.ToJson(responseValue).ToString()
); );
return httpResponse; return httpResponse;
} }
@ -128,7 +126,7 @@ namespace ln.http.resources
private T PostItem(HttpRequest httpRequest) private T PostItem(HttpRequest httpRequest)
{ {
T ni = JsonConvert.DeserializeObject<T>(httpRequest.ContentReader.ReadToEnd()); T ni = JSONMapper.DefaultMapper.FromJson<T>(httpRequest.ContentReader.ReadToEnd());
if (!collection.Insert(ni)) if (!collection.Insert(ni))
throw new HttpException(500, "Object not created, may already exist"); throw new HttpException(500, "Object not created, may already exist");

View File

@ -10,14 +10,12 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json;
using ln.http.exceptions; using ln.http.exceptions;
using ln.logging; using ln.logging;
using Newtonsoft.Json.Linq;
using System.Runtime.CompilerServices;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics; using System.Diagnostics;
using ln.json.mapping;
using ln.json;
namespace ln.http.resources namespace ln.http.resources
{ {
public class CallableAttribute : Attribute public class CallableAttribute : Attribute
@ -152,7 +150,7 @@ namespace ln.http.resources
throw new HttpException("JSON Method call failed, call object not received"); throw new HttpException("JSON Method call failed, call object not received");
} }
MethodCall methodCall = JsonConvert.DeserializeObject<MethodCall>(httpRequest.ContentReader.ReadToEnd()); MethodCall methodCall = JSONMapper.DefaultMapper.FromJson<MethodCall>(httpRequest.ContentReader.ReadToEnd());
MethodResult methodResult; MethodResult methodResult;
try try
@ -170,7 +168,7 @@ namespace ln.http.resources
Logging.Log(e); Logging.Log(e);
} }
String result = JsonConvert.SerializeObject(methodResult); String result = JSONMapper.DefaultMapper.ToJson(methodResult).ToString();
HttpResponse httpResponse = new HttpResponse(httpRequest); HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.SetHeader("content-type", "application/json"); httpResponse.SetHeader("content-type", "application/json");
@ -183,7 +181,7 @@ namespace ln.http.resources
{ {
if (methodResult.Exception != null) if (methodResult.Exception != null)
{ {
return JsonConvert.SerializeObject(methodResult); return JSONMapper.DefaultMapper.ToJson(methodResult).ToString();
} }
else else
{ {
@ -193,11 +191,11 @@ namespace ln.http.resources
private string FlatSerializeResult(MethodResult methodResult) private string FlatSerializeResult(MethodResult methodResult)
{ {
JObject jMethodResult = new JObject(); JSONObject jMethodResult = new JSONObject();
jMethodResult.Add("Exception", null); jMethodResult.Add("Exception", null);
jMethodResult.Add("MethodName", methodResult.MethodName); jMethodResult.Add("MethodName", methodResult.MethodName);
JObject jResult = new JObject(); JSONObject jResult = new JSONObject();
jMethodResult.Add("Result", jResult); jMethodResult.Add("Result", jResult);
Type type = methodResult.GetType(); Type type = methodResult.GetType();
@ -208,11 +206,11 @@ namespace ln.http.resources
if ((fType.IsValueType) || (typeof(string).Equals(fType)) || (fType.IsArray)) if ((fType.IsValueType) || (typeof(string).Equals(fType)) || (fType.IsArray))
{ {
jResult.Add(fieldInfo.Name, new JValue(fieldInfo.GetValue(methodResult.Result))); jResult.Add(fieldInfo.Name, fieldInfo.GetValue(methodResult.Result));
} }
else else
{ {
jResult.Add(fieldInfo.Name, new JValue(fieldInfo.GetValue(methodResult.Result).ToString())); jResult.Add(fieldInfo.Name, fieldInfo.GetValue(methodResult.Result).ToString());
} }
} }
@ -274,7 +272,7 @@ namespace ln.http.resources
try try
{ {
object v = propertyInfo.GetValue(Container); object v = propertyInfo.GetValue(Container);
String result = JsonConvert.SerializeObject(v); String result = JSONMapper.DefaultMapper.ToJson(v).ToString();
HttpResponse httpResponse = new HttpResponse(httpRequest); HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.SetHeader("content-type", "application/json"); httpResponse.SetHeader("content-type", "application/json");

View File

@ -10,9 +10,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Newtonsoft.Json;
using ln.http.exceptions; using ln.http.exceptions;
using Newtonsoft.Json.Linq; using ln.json;
using ln.json.mapping;
namespace ln.http.resources namespace ln.http.resources
{ {
public delegate IEnumerable<T> EnumerationDelegate<T>(); public delegate IEnumerable<T> EnumerationDelegate<T>();
@ -77,9 +77,9 @@ namespace ln.http.resources
{ {
case "GET": case "GET":
T[] resources = Query(httpRequest); T[] resources = Query(httpRequest);
JObject result = new JObject(); JSONObject result = new JSONObject();
foreach (T resource in resources) foreach (T resource in resources)
result.Add(String.Format("{0}/{1}",String.Join("/",Path),GetResourceName(resource)), JObject.FromObject(resource)); result.Add(String.Format("{0}/{1}",String.Join("/",Path),GetResourceName(resource)), JSONMapper.DefaultMapper.ToJson(resource));
responseValue = result; responseValue = result;
break; break;
@ -102,7 +102,7 @@ namespace ln.http.resources
switch (httpRequest.Method) switch (httpRequest.Method)
{ {
case "PUT": case "PUT":
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), resource); JSONMapper.DefaultMapper.Apply(httpRequest.ContentReader.ReadToEnd(), resource);
bool updated = Store(resource); bool updated = Store(resource);
if (updated) if (updated)
@ -121,7 +121,7 @@ namespace ln.http.resources
} }
httpResponse.ContentWriter.Write( httpResponse.ContentWriter.Write(
responseValue is JValue ? (responseValue as JValue).ToString() : JsonConvert.SerializeObject(responseValue) responseValue is JSONValue ? (responseValue as JSONValue).ToString() : JSONMapper.DefaultMapper.ToJson(responseValue).ToString()
); );
return httpResponse; return httpResponse;
} }
@ -139,7 +139,7 @@ namespace ln.http.resources
private T PostItem(HttpRequest httpRequest) private T PostItem(HttpRequest httpRequest)
{ {
T newResource = Create(); T newResource = Create();
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), newResource); JSONMapper.DefaultMapper.Apply(httpRequest.ContentReader.ReadToEnd(), newResource);
if (!Store(newResource)) if (!Store(newResource))
throw new HttpException(500, "Object not created, may already exist"); throw new HttpException(500, "Object not created, may already exist");

View File

@ -8,18 +8,10 @@
// * // *
// **/ // **/
using System; using System;
using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json;
using ln.http.exceptions;
using ln.logging;
using Newtonsoft.Json.Linq;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;
using ln.http.resources.reflection; using ln.http.resources.reflection;
using ln.types.threads; using ln.types.threads;
using ln.json.mapping;
namespace ln.http.resources namespace ln.http.resources
{ {
public class ReflectiveResource : Resource public class ReflectiveResource : Resource
@ -29,13 +21,14 @@ namespace ln.http.resources
Reflector reflector; Reflector reflector;
Object o; Object o;
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(); public JSONMapper JSONMapper { get; private set; }
public ReflectiveResource(Resource container, string name,object o) public ReflectiveResource(Resource container, string name,object o)
: base(container, name) : base(container, name)
{ {
this.o = o; this.o = o;
this.reflector = Reflector.GetReflector(o.GetType()); this.reflector = Reflector.GetReflector(o.GetType());
this.JSONMapper = JSONMapper.DefaultMapper;
} }
public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack) public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack)
@ -49,7 +42,9 @@ namespace ln.http.resources
//response.ContentWriter.Write(jsonValue); //response.ContentWriter.Write(jsonValue);
Timing.Meassure("json converter to stream", () => { Timing.Meassure("json converter to stream", () => {
jsonSerializer.Serialize(response.ContentWriter, currentValue); response.ContentWriter.Write(
JSONMapper.ToJson(currentValue).ToString()
);
}); });

View File

@ -1,13 +1,14 @@
using System; using System;
using Newtonsoft.Json.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using ln.types.json; using ln.json.mapping;
using ln.json;
namespace ln.http.resources.collections namespace ln.http.resources.collections
{ {
public class EntityMapper<TENTITY> public class EntityMapper<TENTITY>
{ {
public JSONConvert JSONConvert { get; set; } public JSONMapper JSONMapper { get; set; }
public Dictionary<string, FieldInfo> publicFields = new Dictionary<string, FieldInfo>(); public Dictionary<string, FieldInfo> publicFields = new Dictionary<string, FieldInfo>();
public Dictionary<string, PropertyInfo> publicProperties = new Dictionary<string, PropertyInfo>(); public Dictionary<string, PropertyInfo> publicProperties = new Dictionary<string, PropertyInfo>();
@ -23,42 +24,31 @@ namespace ln.http.resources.collections
publicFields.Add(fieldInfo.Name, fieldInfo); publicFields.Add(fieldInfo.Name, fieldInfo);
} }
} }
public EntityMapper(JSONConvert jsonConvert) public EntityMapper(JSONMapper jsonMapper)
:this() :this()
{ {
JSONConvert = jsonConvert;
} }
public JObject MapEntity(TENTITY entity) public JSONObject MapEntity(TENTITY entity)
{ {
JObject jObject = new JObject(); JSONObject jObject = new JSONObject();
foreach (FieldInfo fieldInfo in publicFields.Values) foreach (FieldInfo fieldInfo in publicFields.Values)
{ {
JToken jToken = null; JSONValue jValue = JSONMapper.ToJson(fieldInfo.GetValue(entity));
if (JSONConvert.Value2JSON( jObject.Add(fieldInfo.Name, jValue);
fieldInfo.GetValue(entity),
ref jToken
))
{
jObject.Add(fieldInfo.Name, jToken);
}
} }
foreach (PropertyInfo propertyInfo in publicProperties.Values) foreach (PropertyInfo propertyInfo in publicProperties.Values)
{ {
JToken jToken = null; JSONValue jValue = JSONMapper.ToJson(propertyInfo.GetValue(entity));
if (JSONConvert.Value2JSON( jObject.Add(propertyInfo.Name, jValue);
propertyInfo.GetValue(entity),
ref jToken
))
{
jObject.Add(propertyInfo.Name, jToken);
}
} }
return jObject; return jObject;
} }
public bool PopulateEntity(JObject json) public bool PopulateEntity(JSONObject json)
{ {
return false; return false;
} }

View File

@ -12,20 +12,19 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using ln.types.odb; using ln.types.odb;
using Newtonsoft.Json;
using ln.http.exceptions; using ln.http.exceptions;
using Newtonsoft.Json.Linq;
using System.ComponentModel; using System.ComponentModel;
using ln.types.odb.mapped; using ln.types.odb.mapped;
using ln.types.threads; using ln.types.threads;
using ln.types.json;
using System.Reflection; using System.Reflection;
using ln.json.mapping;
using ln.json;
namespace ln.http.resources.collections namespace ln.http.resources.collections
{ {
public class JSONCollectionResource<TENTITY, TIDENT> : Resource where TENTITY : class public class JSONCollectionResource<TENTITY, TIDENT> : Resource where TENTITY : class
{ {
public JSONConvert JSONConvert { get; set; } public JSONMapper JSONMapper { get; set; }
public EntityMapper<TENTITY> EntityMapper { get;} public EntityMapper<TENTITY> EntityMapper { get;}
IEntityCollectionInterface<TENTITY, TIDENT> entityCollectionInterface; IEntityCollectionInterface<TENTITY, TIDENT> entityCollectionInterface;
@ -34,8 +33,8 @@ namespace ln.http.resources.collections
: base(container, typeof(TENTITY).Name) : base(container, typeof(TENTITY).Name)
{ {
this.entityCollectionInterface = entityCollectionInterface; this.entityCollectionInterface = entityCollectionInterface;
JSONConvert = JSONConvert.DefaultInstance; this.JSONMapper = JSONMapper.DefaultMapper;
EntityMapper = new EntityMapper<TENTITY>(JSONConvert); EntityMapper = new EntityMapper<TENTITY>(JSONMapper);
} }
public override bool HandlesDispatching => true; public override bool HandlesDispatching => true;
@ -75,7 +74,7 @@ namespace ln.http.resources.collections
else else
{ {
String documentID = pathStack.Dequeue(); String documentID = pathStack.Dequeue();
TIDENT ident = this.JSONConvert.JSON2Value<TIDENT>(new JValue(documentID)); TIDENT ident = this.JSONMapper.FromJson<TIDENT>(this.JSONMapper.ToJson(documentID));
TENTITY entity = entityCollectionInterface.GetEntity(ident); TENTITY entity = entityCollectionInterface.GetEntity(ident);
switch (httpRequest.Method) switch (httpRequest.Method)
@ -98,32 +97,33 @@ namespace ln.http.resources.collections
private CollectionResult Describe(HttpRequest request) private CollectionResult Describe(HttpRequest request)
{ {
CollectionResult collectionResult = new CollectionResult(); CollectionResult collectionResult = new CollectionResult();
collectionResult.Descriptor = new JObject(); collectionResult.Descriptor = new JSONObject();
JArray jProperties = new JArray(); JSONArray jProperties = new JSONArray();
foreach (FieldInfo fieldInfo in EntityMapper.publicFields.Values) /* ToDo: Implement or remove */
{ //foreach (FieldInfo fieldInfo in EntityMapper.publicFields.Values)
String skyType = JSONConvert.GetSkyType(fieldInfo.FieldType); //{
if (skyType != null) // String skyType = JSONMapper.GetSkyType(fieldInfo.FieldType);
{ // if (skyType != null)
JObject field = new JObject(); // {
field.Add("name", JToken.FromObject(fieldInfo.Name)); // JObject field = new JObject();
field.Add("type", JToken.FromObject(skyType)); // field.Add("name", JToken.FromObject(fieldInfo.Name));
jProperties.Add(field); // field.Add("type", JToken.FromObject(skyType));
} // jProperties.Add(field);
} // }
foreach (PropertyInfo propertyInfo in EntityMapper.publicProperties.Values) //}
{ //foreach (PropertyInfo propertyInfo in EntityMapper.publicProperties.Values)
String skyType = JSONConvert.GetSkyType(propertyInfo.PropertyType); //{
if (skyType != null) // String skyType = JSONConvert.GetSkyType(propertyInfo.PropertyType);
{ // if (skyType != null)
JObject field = new JObject(); // {
field.Add("name", JToken.FromObject(propertyInfo.Name)); // JObject field = new JObject();
field.Add("type", JToken.FromObject(skyType)); // field.Add("name", JToken.FromObject(propertyInfo.Name));
jProperties.Add(field); // field.Add("type", JToken.FromObject(skyType));
} // jProperties.Add(field);
} // }
//}
collectionResult.Descriptor.Add("properties", jProperties); collectionResult.Descriptor.Add("properties", jProperties);
return collectionResult; return collectionResult;
@ -148,8 +148,8 @@ namespace ln.http.resources.collections
{ {
HttpResponse response = new HttpResponse(request); HttpResponse response = new HttpResponse(request);
JObject surround = new JObject(); JSONObject surround = new JSONObject();
JObject jvalues = new JObject(); JSONObject jvalues = new JSONObject();
foreach (TENTITY entity in collectionResult.Values) foreach (TENTITY entity in collectionResult.Values)
{ {
@ -158,10 +158,10 @@ namespace ln.http.resources.collections
} }
surround.Add("values", jvalues); surround.Add("values", jvalues);
JObject jfailed = new JObject(); JSONObject jfailed = new JSONObject();
foreach (TIDENT id in collectionResult.Failed.Keys) foreach (TIDENT id in collectionResult.Failed.Keys)
{ {
jfailed.Add(id.ToString(), collectionResult.Failed[id]); jfailed.Add(id.ToString(), new JSONString(collectionResult.Failed[id]));
} }
surround.Add("failed", jfailed); surround.Add("failed", jfailed);
@ -181,7 +181,7 @@ namespace ln.http.resources.collections
public List<TENTITY> Values { get; } = new List<TENTITY>(); public List<TENTITY> Values { get; } = new List<TENTITY>();
public Dictionary<TIDENT, String> Failed { get; } = new Dictionary<TIDENT, string>(); public Dictionary<TIDENT, String> Failed { get; } = new Dictionary<TIDENT, string>();
public JObject Descriptor { get; set; } public JSONObject Descriptor { get; set; }
} }
} }

View File

@ -28,9 +28,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@ -69,6 +66,10 @@
<Project>{8D9AB9A5-E513-4BA7-A450-534F6456BF28}</Project> <Project>{8D9AB9A5-E513-4BA7-A450-534F6456BF28}</Project>
<Name>ln.types</Name> <Name>ln.types</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\ln.json\ln.json.csproj">
<Project>{D9342117-3249-4D8B-87C9-51A50676B158}</Project>
<Name>ln.json</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="reflection\" /> <Folder Include="reflection\" />
@ -77,7 +78,6 @@
<Folder Include="websocket\" /> <Folder Include="websocket\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" />
<None Include="doc\JSONCollection.txt" /> <None Include="doc\JSONCollection.txt" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net47" />
</packages>