diff --git a/CollectionResource.cs b/CollectionResource.cs index ce4d90c..5483b14 100644 --- a/CollectionResource.cs +++ b/CollectionResource.cs @@ -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 : 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(httpRequest.ContentReader.ReadToEnd()); + T ni = JSONMapper.DefaultMapper.FromJson(httpRequest.ContentReader.ReadToEnd()); if (!collection.Insert(ni)) throw new HttpException(500, "Object not created, may already exist"); diff --git a/JsonCallResource.cs b/JsonCallResource.cs index 2c164d7..f600586 100644 --- a/JsonCallResource.cs +++ b/JsonCallResource.cs @@ -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(httpRequest.ContentReader.ReadToEnd()); + MethodCall methodCall = JSONMapper.DefaultMapper.FromJson(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"); diff --git a/ObjectContainerResource.cs b/ObjectContainerResource.cs index 69e1cd2..6e9120a 100644 --- a/ObjectContainerResource.cs +++ b/ObjectContainerResource.cs @@ -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 EnumerationDelegate(); @@ -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"); diff --git a/ReflectiveResource.cs b/ReflectiveResource.cs index 5741d09..0bc3fea 100644 --- a/ReflectiveResource.cs +++ b/ReflectiveResource.cs @@ -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 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() + ); }); diff --git a/collections/EntityMapper.cs b/collections/EntityMapper.cs index c52d3cb..409e395 100644 --- a/collections/EntityMapper.cs +++ b/collections/EntityMapper.cs @@ -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 { - public JSONConvert JSONConvert { get; set; } + public JSONMapper JSONMapper { get; set; } public Dictionary publicFields = new Dictionary(); public Dictionary publicProperties = new Dictionary(); @@ -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; } diff --git a/collections/JSONCollectionResource.cs b/collections/JSONCollectionResource.cs index cfa5891..f8aae5e 100644 --- a/collections/JSONCollectionResource.cs +++ b/collections/JSONCollectionResource.cs @@ -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 : Resource where TENTITY : class { - public JSONConvert JSONConvert { get; set; } + public JSONMapper JSONMapper { get; set; } public EntityMapper EntityMapper { get;} IEntityCollectionInterface entityCollectionInterface; @@ -34,8 +33,8 @@ namespace ln.http.resources.collections : base(container, typeof(TENTITY).Name) { this.entityCollectionInterface = entityCollectionInterface; - JSONConvert = JSONConvert.DefaultInstance; - EntityMapper = new EntityMapper(JSONConvert); + this.JSONMapper = JSONMapper.DefaultMapper; + EntityMapper = new EntityMapper(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(new JValue(documentID)); + TIDENT ident = this.JSONMapper.FromJson(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 Values { get; } = new List(); public Dictionary Failed { get; } = new Dictionary(); - public JObject Descriptor { get; set; } + public JSONObject Descriptor { get; set; } } } diff --git a/ln.http.resources.csproj b/ln.http.resources.csproj index 39fce06..343ddef 100644 --- a/ln.http.resources.csproj +++ b/ln.http.resources.csproj @@ -28,9 +28,6 @@ - - ..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll - @@ -69,6 +66,10 @@ {8D9AB9A5-E513-4BA7-A450-534F6456BF28} ln.types + + {D9342117-3249-4D8B-87C9-51A50676B158} + ln.json + @@ -77,7 +78,6 @@ - diff --git a/packages.config b/packages.config deleted file mode 100644 index 39d1cce..0000000 --- a/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file