Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active Nov 17, 2016
Embed
What would you like to do?
class MyConverter : Newtonsoft.Json.Converters.CustomCreationConverter<IDictionary<string, object>>
{
public override IDictionary<string, object> Create(Type objectType)
{
return new Dictionary<string, object>();
}
public override bool CanConvert(Type objectType)
{
// in addition to handling IDictionary<string, object>
// we want to handle the deserialization of dict value
// which is of type object
return objectType == typeof(object) || base.CanConvert(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject
|| reader.TokenType == JsonToken.Null)
return base.ReadJson(reader, objectType, existingValue, serializer);
// if the next token is not an object
// then fall back on standard deserializer (strings, numbers etc.)
return serializer.Deserialize(reader);
}
}
string convert(string json) {
var pars = JsonConvert.DeserializeObject<IDictionary<string, object>>(
json, new JsonConverter[] { new MyConverter() });
var hash = DL.Hash.FromDictionary(pars);
return liquidTemplate.Render(hash);
}
{
"elements":{
"droppedElements":[
{
"originalScale":{
"x":1,
"y":1,
"z":1
},
"screenScale":0.002,
"isAttachedToDropTarget":true,
"allowedDropTargets":[
],
"extendDropArea":{
"min":{
"x":5,
"y":-10
},
"max":{
"x":10,
"y":10
}
},
"localBoundingBox":{
"min":{
"x":-95.97487369179726,
"y":-44.47487369179726,
"z":-8.25
},
"max":{
"x":95.97487369179728,
"y":44.4748736917973,
"z":8.25
}
},
"elementId":"Door right",
"shouldClone":false,
"screenPosition":{
"x":0.5666666666666667,
"y":-0.09999999999999998
},
"position":[
-11.572050899031623,
68.27981366285937,
0
],
"dropTargetId":"shedWalls",
"dropTargetName":"front"
},
{
"originalScale":{
"x":1,
"y":1,
"z":1
},
"screenScale":0.002,
"isAttachedToDropTarget":true,
"allowedDropTargets":[
],
"extendDropArea":{
"min":{
"x":-10,
"y":-10
},
"max":{
"x":10,
"y":10
}
},
"localBoundingBox":{
"min":{
"x":-45.97487369179726,
"y":-25.974873691797256,
"z":-2.0000000000000147
},
"max":{
"x":45.97487369179727,
"y":25.974873691797278,
"z":2.0000000000000004
}
},
"elementId":"Rotating-top window, 6 squares",
"shouldClone":false,
"screenPosition":{
"x":0.5687500000000001,
"y":0.012594458438287104
},
"position":[
50.261852194593565,
-74.14660418899098,
0
],
"dropTargetId":"shedWalls",
"dropTargetName":"front"
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment