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

View File

@ -10,14 +10,12 @@
using System;
using System.Reflection;
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.json.mapping;
using ln.json;
namespace ln.http.resources
{
public class CallableAttribute : Attribute
@ -152,7 +150,7 @@ namespace ln.http.resources
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;
try
@ -170,7 +168,7 @@ namespace ln.http.resources
Logging.Log(e);
}
String result = JsonConvert.SerializeObject(methodResult);
String result = JSONMapper.DefaultMapper.ToJson(methodResult).ToString();
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.SetHeader("content-type", "application/json");
@ -183,7 +181,7 @@ namespace ln.http.resources
{
if (methodResult.Exception != null)
{
return JsonConvert.SerializeObject(methodResult);
return JSONMapper.DefaultMapper.ToJson(methodResult).ToString();
}
else
{
@ -193,11 +191,11 @@ namespace ln.http.resources
private string FlatSerializeResult(MethodResult methodResult)
{
JObject jMethodResult = new JObject();
JSONObject jMethodResult = new JSONObject();
jMethodResult.Add("Exception", null);
jMethodResult.Add("MethodName", methodResult.MethodName);
JObject jResult = new JObject();
JSONObject jResult = new JSONObject();
jMethodResult.Add("Result", jResult);
Type type = methodResult.GetType();
@ -208,11 +206,11 @@ namespace ln.http.resources
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
{
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
{
object v = propertyInfo.GetValue(Container);
String result = JsonConvert.SerializeObject(v);
String result = JSONMapper.DefaultMapper.ToJson(v).ToString();
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.SetHeader("content-type", "application/json");

View File

@ -10,9 +10,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using ln.http.exceptions;
using Newtonsoft.Json.Linq;
using ln.json;
using ln.json.mapping;
namespace ln.http.resources
{
public delegate IEnumerable<T> EnumerationDelegate<T>();
@ -77,9 +77,9 @@ namespace ln.http.resources
{
case "GET":
T[] resources = Query(httpRequest);
JObject result = new JObject();
JSONObject result = new JSONObject();
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;
break;
@ -102,7 +102,7 @@ namespace ln.http.resources
switch (httpRequest.Method)
{
case "PUT":
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), resource);
JSONMapper.DefaultMapper.Apply(httpRequest.ContentReader.ReadToEnd(), resource);
bool updated = Store(resource);
if (updated)
@ -121,7 +121,7 @@ namespace ln.http.resources
}
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;
}
@ -139,7 +139,7 @@ namespace ln.http.resources
private T PostItem(HttpRequest httpRequest)
{
T newResource = Create();
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), newResource);
JSONMapper.DefaultMapper.Apply(httpRequest.ContentReader.ReadToEnd(), newResource);
if (!Store(newResource))
throw new HttpException(500, "Object not created, may already exist");

View File

@ -8,18 +8,10 @@
// *
// **/
using System;
using System.Reflection;
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.types.threads;
using ln.json.mapping;
namespace ln.http.resources
{
public class ReflectiveResource : Resource
@ -29,13 +21,14 @@ namespace ln.http.resources
Reflector reflector;
Object o;
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault();
public JSONMapper JSONMapper { get; private set; }
public ReflectiveResource(Resource container, string name,object o)
: base(container, name)
{
this.o = o;
this.reflector = Reflector.GetReflector(o.GetType());
this.JSONMapper = JSONMapper.DefaultMapper;
}
public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack)
@ -49,7 +42,9 @@ namespace ln.http.resources
//response.ContentWriter.Write(jsonValue);
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 Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Reflection;
using ln.types.json;
using ln.json.mapping;
using ln.json;
namespace ln.http.resources.collections
{
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, PropertyInfo> publicProperties = new Dictionary<string, PropertyInfo>();
@ -23,42 +24,31 @@ namespace ln.http.resources.collections
publicFields.Add(fieldInfo.Name, fieldInfo);
}
}
public EntityMapper(JSONConvert jsonConvert)
public EntityMapper(JSONMapper jsonMapper)
: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)
{
JToken jToken = null;
if (JSONConvert.Value2JSON(
fieldInfo.GetValue(entity),
ref jToken
))
{
jObject.Add(fieldInfo.Name, jToken);
}
JSONValue jValue = JSONMapper.ToJson(fieldInfo.GetValue(entity));
jObject.Add(fieldInfo.Name, jValue);
}
foreach (PropertyInfo propertyInfo in publicProperties.Values)
{
JToken jToken = null;
if (JSONConvert.Value2JSON(
propertyInfo.GetValue(entity),
ref jToken
))
{
jObject.Add(propertyInfo.Name, jToken);
}
JSONValue jValue = JSONMapper.ToJson(propertyInfo.GetValue(entity));
jObject.Add(propertyInfo.Name, jValue);
}
return jObject;
}
public bool PopulateEntity(JObject json)
public bool PopulateEntity(JSONObject json)
{
return false;
}

View File

@ -12,20 +12,19 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using ln.types.odb;
using Newtonsoft.Json;
using ln.http.exceptions;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using ln.types.odb.mapped;
using ln.types.threads;
using ln.types.json;
using System.Reflection;
using ln.json.mapping;
using ln.json;
namespace ln.http.resources.collections
{
public class JSONCollectionResource<TENTITY, TIDENT> : Resource where TENTITY : class
{
public JSONConvert JSONConvert { get; set; }
public JSONMapper JSONMapper { get; set; }
public EntityMapper<TENTITY> EntityMapper { get;}
IEntityCollectionInterface<TENTITY, TIDENT> entityCollectionInterface;
@ -34,8 +33,8 @@ namespace ln.http.resources.collections
: base(container, typeof(TENTITY).Name)
{
this.entityCollectionInterface = entityCollectionInterface;
JSONConvert = JSONConvert.DefaultInstance;
EntityMapper = new EntityMapper<TENTITY>(JSONConvert);
this.JSONMapper = JSONMapper.DefaultMapper;
EntityMapper = new EntityMapper<TENTITY>(JSONMapper);
}
public override bool HandlesDispatching => true;
@ -75,7 +74,7 @@ namespace ln.http.resources.collections
else
{
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);
switch (httpRequest.Method)
@ -98,32 +97,33 @@ namespace ln.http.resources.collections
private CollectionResult Describe(HttpRequest request)
{
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)
{
String skyType = JSONConvert.GetSkyType(fieldInfo.FieldType);
if (skyType != null)
{
JObject field = new JObject();
field.Add("name", JToken.FromObject(fieldInfo.Name));
field.Add("type", JToken.FromObject(skyType));
jProperties.Add(field);
}
}
foreach (PropertyInfo propertyInfo in EntityMapper.publicProperties.Values)
{
String skyType = JSONConvert.GetSkyType(propertyInfo.PropertyType);
if (skyType != null)
{
JObject field = new JObject();
field.Add("name", JToken.FromObject(propertyInfo.Name));
field.Add("type", JToken.FromObject(skyType));
jProperties.Add(field);
}
}
/* ToDo: Implement or remove */
//foreach (FieldInfo fieldInfo in EntityMapper.publicFields.Values)
//{
// String skyType = JSONMapper.GetSkyType(fieldInfo.FieldType);
// if (skyType != null)
// {
// JObject field = new JObject();
// field.Add("name", JToken.FromObject(fieldInfo.Name));
// field.Add("type", JToken.FromObject(skyType));
// jProperties.Add(field);
// }
//}
//foreach (PropertyInfo propertyInfo in EntityMapper.publicProperties.Values)
//{
// String skyType = JSONConvert.GetSkyType(propertyInfo.PropertyType);
// if (skyType != null)
// {
// JObject field = new JObject();
// field.Add("name", JToken.FromObject(propertyInfo.Name));
// field.Add("type", JToken.FromObject(skyType));
// jProperties.Add(field);
// }
//}
collectionResult.Descriptor.Add("properties", jProperties);
return collectionResult;
@ -148,8 +148,8 @@ namespace ln.http.resources.collections
{
HttpResponse response = new HttpResponse(request);
JObject surround = new JObject();
JObject jvalues = new JObject();
JSONObject surround = new JSONObject();
JSONObject jvalues = new JSONObject();
foreach (TENTITY entity in collectionResult.Values)
{
@ -158,10 +158,10 @@ namespace ln.http.resources.collections
}
surround.Add("values", jvalues);
JObject jfailed = new JObject();
JSONObject jfailed = new JSONObject();
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);
@ -181,7 +181,7 @@ namespace ln.http.resources.collections
public List<TENTITY> Values { get; } = new List<TENTITY>();
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>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@ -69,6 +66,10 @@
<Project>{8D9AB9A5-E513-4BA7-A450-534F6456BF28}</Project>
<Name>ln.types</Name>
</ProjectReference>
<ProjectReference Include="..\ln.json\ln.json.csproj">
<Project>{D9342117-3249-4D8B-87C9-51A50676B158}</Project>
<Name>ln.json</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="reflection\" />
@ -77,7 +78,6 @@
<Folder Include="websocket\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="doc\JSONCollection.txt" />
</ItemGroup>
<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>