forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceStack.Pcl.Android
176 lines (149 loc) · 5.73 KB
/
ServiceStack.Pcl.Android
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//Copyright (c) Service Stack LLC. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
#if !PCL
using System;
using System.Collections.Generic;
using System.Dynamic;
using ServiceStack.Text;
using ServiceStack.Text.Common;
using ServiceStack.Text.Json;
using System.Linq;
using System.Text;
namespace ServiceStack
{
public static class DeserializeDynamic<TSerializer>
where TSerializer : ITypeSerializer
{
private static readonly ITypeSerializer Serializer = JsWriter.GetTypeSerializer<TSerializer>();
private static readonly ParseStringDelegate CachedParseFn;
static DeserializeDynamic()
{
CachedParseFn = ParseDynamic;
}
public static ParseStringDelegate Parse
{
get { return CachedParseFn; }
}
public static IDynamicMetaObjectProvider ParseDynamic(string value)
{
var index = VerifyAndGetStartIndex(value, typeof(ExpandoObject));
var result = new ExpandoObject();
if (JsonTypeSerializer.IsEmptyMap(value)) return result;
var container = (IDictionary<String, Object>)result;
var tryToParsePrimitiveTypes = JsConfig.TryToParsePrimitiveTypeValues;
var valueLength = value.Length;
while (index < valueLength)
{
var keyValue = Serializer.EatMapKey(value, ref index);
Serializer.EatMapKeySeperator(value, ref index);
var elementValue = Serializer.EatValue(value, ref index);
var mapKey = Serializer.UnescapeString(keyValue);
if (JsonUtils.IsJsObject(elementValue))
{
container[mapKey] = ParseDynamic(elementValue);
}
else if (JsonUtils.IsJsArray(elementValue))
{
container[mapKey] = DeserializeList<List<object>, TSerializer>.Parse(elementValue);
}
else if (tryToParsePrimitiveTypes)
{
container[mapKey] = DeserializeType<TSerializer>.ParsePrimitive(elementValue) ?? Serializer.UnescapeString(elementValue);
}
else
{
container[mapKey] = Serializer.UnescapeString(elementValue);
}
Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
}
return result;
}
private static int VerifyAndGetStartIndex(string value, Type createMapType)
{
var index = 0;
if (!Serializer.EatMapStartChar(value, ref index))
{
//Don't throw ex because some KeyValueDataContractDeserializer don't have '{}'
Tracer.Instance.WriteDebug("WARN: Map definitions should start with a '{0}', expecting serialized type '{1}', got string starting with: {2}",
JsWriter.MapStartChar, createMapType != null ? createMapType.Name : "Dictionary<,>", value.Substring(0, value.Length < 50 ? value.Length : 50));
}
return index;
}
}
public class DynamicJson : DynamicObject
{
private readonly IDictionary<string, object> _hash = new Dictionary<string, object>();
public static string Serialize(dynamic instance)
{
var json = JsonSerializer.SerializeToString(instance);
return json;
}
public static dynamic Deserialize(string json)
{
// Support arbitrary nesting by using JsonObject
var deserialized = JsonSerializer.DeserializeFromString<JsonObject>(json);
var hash = deserialized.ToDictionary<KeyValuePair<string, string>, string, object>(entry => entry.Key, entry => entry.Value);
return new DynamicJson(hash);
}
public DynamicJson(IEnumerable<KeyValuePair<string, object>> hash)
{
_hash.Clear();
foreach (var entry in hash)
{
_hash.Add(Underscored(entry.Key), entry.Value);
}
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
var name = Underscored(binder.Name);
_hash[name] = value;
return _hash[name] == value;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var name = Underscored(binder.Name);
return YieldMember(name, out result);
}
public override string ToString()
{
return JsonSerializer.SerializeToString(_hash);
}
private bool YieldMember(string name, out object result)
{
if (_hash.ContainsKey(name))
{
var json = _hash[name].ToString();
if (json.TrimStart(' ').StartsWith("{", StringComparison.Ordinal))
{
result = Deserialize(json);
return true;
}
result = json;
return _hash[name] == result;
}
result = null;
return false;
}
internal static string Underscored(string pascalCase)
{
return Underscored(pascalCase.ToCharArray());
}
internal static string Underscored(IEnumerable<char> pascalCase)
{
var sb = new StringBuilder();
var i = 0;
foreach (var c in pascalCase)
{
if (char.IsUpper(c) && i > 0)
{
sb.Append("_");
}
sb.Append(c);
i++;
}
return sb.ToString().ToLowerInvariant();
}
}
}
#endif