Skip to content

Commit 9bff34d

Browse files
committed
first pass at deserializer
1 parent dc93177 commit 9bff34d

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

JsonApiDotNetCore/Abstractions/ModelAccessor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using JsonApiDotNetCore.Extensions;
6+
using JsonApiDotNetCore.JsonApi;
57

68
namespace JsonApiDotNetCore.Abstractions
79
{
@@ -18,5 +20,22 @@ public static Type GetTypeFromModelRelationshipName(Type modelType, string relat
1820
}
1921
return relationshipType;
2022
}
23+
24+
public static object SetValuesOnModelInstance(object model, Dictionary<string, object> jsonApiAttributes)
25+
{
26+
var modelProperties = model.GetType().GetProperties().ToList();
27+
foreach (var attribute in jsonApiAttributes)
28+
{
29+
modelProperties.ForEach(pI =>
30+
{
31+
if (pI.Name.ToProperCase() == attribute.Key.ToProperCase())
32+
{
33+
var convertedValue = Convert.ChangeType(attribute.Value, pI.PropertyType);
34+
pI.SetValue(model, convertedValue);
35+
}
36+
});
37+
}
38+
return model;
39+
}
2140
}
2241
}

JsonApiDotNetCore/JsonApi/DocumentBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ private Dictionary<string, string> BuildSelfLink(string protocol, string host, s
105105
};
106106
}
107107

108+
private Dictionary<string, string> GetRelationshipLinks(IJsonApiResource resource, string relationshipName)
109+
{
110+
return BuildRelationshipLinks(_context.HttpContext.Request.Scheme,
111+
_context.HttpContext.Request.Host.ToString(), _context.Configuration.Namespace,
112+
_context.GetEntityName(), resource.Id, relationshipName);
113+
}
114+
108115
private Dictionary<string, string> BuildRelationshipLinks(string protocol, string host, string nameSpace, string resourceCollectionName, string resourceId, string relationshipName)
109116
{
110117
return new Dictionary<string, string>
@@ -113,12 +120,5 @@ private Dictionary<string, string> BuildRelationshipLinks(string protocol, strin
113120
{"related", $"{protocol}://{host}/{nameSpace}/{resourceCollectionName}/{resourceId}/{relationshipName}"}
114121
};
115122
}
116-
117-
private Dictionary<string, string> GetRelationshipLinks(IJsonApiResource resource, string relationshipName)
118-
{
119-
return BuildRelationshipLinks(_context.HttpContext.Request.Scheme,
120-
_context.HttpContext.Request.Host.ToString(), _context.Configuration.Namespace,
121-
_context.GetEntityName(), resource.Id, relationshipName);
122-
}
123123
}
124124
}

JsonApiDotNetCore/JsonApi/JsonApiDocument.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace JsonApiDotNetCore.JsonApi
44
{
5-
public class JsonApiDocument
6-
{
7-
public Dictionary<string, string> Links { get; set; }
8-
public object Data { get; set; }
9-
}
5+
public class JsonApiDocument
6+
{
7+
public Dictionary<string, string> Links { get; set; }
8+
// Data could be List<JsonApiDatum> or JsonApiDatum
9+
public object Data { get; set; }
10+
}
1011
}

JsonApiDotNetCore/Routing/Router.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private ObjectResult ActivateControllerMethod(JsonApiController controller)
5656
case "GET":
5757
return string.IsNullOrEmpty(route.ResourceId) ? controller.Get() : controller.Get(route.ResourceId);
5858
case "POST":
59-
return controller.Post(null);
59+
return controller.Post(new JsonApiDeserializer(_jsonApiContext).GetEntity());
6060
case "PUT":
6161
return controller.Put(route.ResourceId, null);
6262
case "DELETE":
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
using System;
2+
using System.IO;
23
using JsonApiDotNetCore.Abstractions;
34
using JsonApiDotNetCore.Configuration;
5+
using JsonApiDotNetCore.JsonApi;
6+
using Newtonsoft.Json;
47

58
namespace JsonApiDotNetCore.Services
69
{
710
public class JsonApiDeserializer
811
{
912
private readonly JsonApiContext _context;
10-
private readonly JsonApiModelConfiguration _jsonApiModelConfiguration;
11-
private readonly string _entityName;
12-
private readonly Type _entityType;
13-
public JsonApiDeserializer(JsonApiContext jsonApiContext, JsonApiModelConfiguration configuration)
13+
public JsonApiDeserializer(JsonApiContext jsonApiContext)
1414
{
1515
_context = jsonApiContext;
16-
_jsonApiModelConfiguration = configuration;
17-
_entityName = jsonApiContext.GetEntityName();
18-
_entityType = jsonApiContext.GetEntityType();
16+
}
17+
18+
public object GetEntityFromRequest()
19+
{
20+
var body = GetRequestBody(_context.HttpContext.Request.Body);
21+
var document = JsonConvert.DeserializeObject<JsonApiDocument>(body);
22+
var entity = Activator.CreateInstance(_context.GetEntityType());
23+
return ModelAccessor.SetValuesOnModelInstance(entity, ((JsonApiDatum) document.Data).Attributes);
24+
}
25+
26+
private static string GetRequestBody(Stream body)
27+
{
28+
using (var reader = new StreamReader(body))
29+
{
30+
return reader.ReadToEnd();
31+
}
1932
}
2033
}
2134
}

0 commit comments

Comments
 (0)