Esempio n. 1
0
        public void AddTypeResolver(IJsonRpcTypeResolver resolver)
        {
            if (resolvers == null)
                throw new ArgumentNullException("resolver");

            for (int i = 0; i < resolvers.Count; i++) {
                if (resolvers[i].GetType() == resolver.GetType())
                    throw new ArgumentException("Another resolver of type '" + resolver.GetType() + "' was already present.");
            }

            resolvers.Add(resolver);
        }
Esempio n. 2
0
        private Type ResolveType(string typeName, out IJsonRpcTypeResolver resolver)
        {
            for (int i = 0; i < resolvers.Count; i++) {
                Type type;
                resolver = resolvers[i];
                if ((type = resolver.ResolveType(typeName)) != null) {
                    return type;
                }
            }

            resolver = null;
            return null;
        }
Esempio n. 3
0
        private string ResolveTypeName(object value, out IJsonRpcTypeResolver resolver)
        {
            for (int i = 0; i < resolvers.Count; i++) {
                string elementName;
                resolver = resolvers[i];
                if (!String.IsNullOrEmpty(elementName = resolver.ResolveTypeName(value.GetType()))) {
                    return elementName;
                }
            }

            resolver = null;
            return null;
        }
Esempio n. 4
0
        private void ReadInto(JsonReader reader, JsonWriter jsonWriter, bool firstLevel, out Type type, out IJsonRpcTypeResolver resolver)
        {
            type = null;
            resolver = null;

            while (reader.Read()) {
                if (reader.Token == JsonToken.PropertyName) {
                    string propertyName = (string)reader.Value;

                    if (firstLevel && propertyName == "$type") {
                        if (!reader.Read())
                            throw new FormatException();
                        if (reader.Token != JsonToken.String)
                            throw new FormatException();

                        string typeString = (string)reader.Value;

                        type = ResolveType(typeString, out resolver);
                        if (type == null)
                            throw new FormatException();
                    } else {
                        jsonWriter.WritePropertyName(propertyName);
                    }
                } else if (reader.Token == JsonToken.Boolean) {
                    jsonWriter.Write((bool)reader.Value);
                } else if (reader.Token == JsonToken.Int) {
                    jsonWriter.Write((int)reader.Value);
                } else if (reader.Token == JsonToken.Long) {
                    jsonWriter.Write((long)reader.Value);
                } else if (reader.Token == JsonToken.Double) {
                    jsonWriter.Write((double)reader.Value);
                } else if (reader.Token == JsonToken.String) {
                    jsonWriter.Write((string)reader.Value);
                } else if (reader.Token == JsonToken.Null) {
                    jsonWriter.Write(null);
                } else if (reader.Token == JsonToken.ArrayStart) {
                    jsonWriter.WriteArrayStart();

                    while (reader.Read()) {
                        Type dummyType;
                        IJsonRpcTypeResolver dummyResolver;
                        ReadInto(reader, jsonWriter, false, out dummyType, out dummyResolver);
                        if (reader.Token == JsonToken.ArrayEnd) {
                            jsonWriter.WriteArrayEnd();
                            break;
                        }
                    }
                } else if (reader.Token == JsonToken.ObjectEnd) {
                    jsonWriter.WriteObjectEnd();
                    break;
                }
            }
        }