public static JsonObject ParseJsonObject(string value) { var index = VerifyAndGetStartIndex(value, typeof(JsonObject)); var result = new JsonObject(); if (JsonTypeSerializer.IsEmptyMap(value, index)) { return(result); } 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); if (keyValue == null) { continue; } var mapKey = keyValue; var mapValue = elementValue; result[mapKey] = mapValue; Serializer.EatItemSeperatorOrMapEndChar(value, ref index); } return(result); }
public static IDictionary ParseIDictionary(string value, Type dictType) { if (value == null) { return(null); } var index = VerifyAndGetStartIndex(value, dictType); var valueParseMethod = Serializer.GetParseFn(typeof(object)); if (valueParseMethod == null) { return(null); } var to = (IDictionary)dictType.CreateInstance(); if (JsonTypeSerializer.IsEmptyMap(value, index)) { return(to); } var valueLength = value.Length; while (index < valueLength) { var keyValue = Serializer.EatMapKey(value, ref index); Serializer.EatMapKeySeperator(value, ref index); var elementStartIndex = index; var elementValue = Serializer.EatTypeValue(value, ref index); if (keyValue == null) { continue; } var mapKey = valueParseMethod(keyValue); if (elementStartIndex < valueLength) { Serializer.EatWhitespace(value, ref elementStartIndex); to[mapKey] = DeserializeType <TSerializer> .ParsePrimitive(elementValue, value[elementStartIndex]); } else { to[mapKey] = valueParseMethod(elementValue); } Serializer.EatItemSeperatorOrMapEndChar(value, ref index); } return(to); }
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); }
public static object ParseKeyValuePair <TKey, TValue>( string value, Type createMapType, ParseStringDelegate parseKeyFn, ParseStringDelegate parseValueFn) { if (value == null) { return(default(KeyValuePair <TKey, TValue>)); } var index = VerifyAndGetStartIndex(value, createMapType); if (JsonTypeSerializer.IsEmptyMap(value, index)) { return(new KeyValuePair <TKey, TValue>()); } var keyValue = default(TKey); var valueValue = default(TValue); var valueLength = value.Length; while (index < valueLength) { var key = Serializer.EatMapKey(value, ref index); Serializer.EatMapKeySeperator(value, ref index); var keyElementValue = Serializer.EatTypeValue(value, ref index); if (key.CompareIgnoreCase("key") == 0) { keyValue = (TKey)parseKeyFn(keyElementValue); } else if (key.CompareIgnoreCase("value") == 0) { valueValue = (TValue)parseValueFn(keyElementValue); } else { throw new SerializationException("Incorrect KeyValuePair property: " + key); } Serializer.EatItemSeperatorOrMapEndChar(value, ref index); } return(new KeyValuePair <TKey, TValue>(keyValue, valueValue)); }
public static Dictionary <string, string> ParseStringDictionary(string value) { if (value == null) { return(null); } var index = VerifyAndGetStartIndex(value, typeof(Dictionary <string, string>)); var result = new Dictionary <string, string>(); if (JsonTypeSerializer.IsEmptyMap(value, index)) { return(result); } 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); if (keyValue == null) { continue; } var mapKey = Serializer.UnescapeString(keyValue); var mapValue = Serializer.UnescapeString(elementValue); result[mapKey] = mapValue; Serializer.EatItemSeperatorOrMapEndChar(value, ref index); } return(result); }
public static object ParseTuple(Type tupleType, string value) { var index = 0; Serializer.EatMapStartChar(value, ref index); if (JsonTypeSerializer.IsEmptyMap(value, index)) { return(tupleType.CreateInstance()); } var genericArgs = tupleType.GetGenericArguments(); var argValues = new object[genericArgs.Length]; 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); if (keyValue == null) { continue; } var keyIndex = keyValue.Substring("Item".Length).ToInt() - 1; var parseFn = Serializer.GetParseFn(genericArgs[keyIndex]); argValues[keyIndex] = parseFn(elementValue); Serializer.EatItemSeperatorOrMapEndChar(value, ref index); } var ctor = tupleType.DeclaredConstructors() .First(x => x.GetParameters().Length == genericArgs.Length); return(ctor.Invoke(argValues)); }
internal static object StringToType( TypeConfig typeConfig, string strType, EmptyCtorDelegate ctorFn, Dictionary <string, TypeAccessor> typeAccessorMap) { var index = 0; var type = typeConfig.Type; if (strType == null) { return(null); } //if (!Serializer.EatMapStartChar(strType, ref index)) for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) { break; } } //Whitespace inline if (strType[index++] != JsWriter.MapStartChar) { throw DeserializeTypeRef.CreateSerializationError(type, strType); } if (JsonTypeSerializer.IsEmptyMap(strType, index)) { return(ctorFn()); } object instance = null; var propertyResolver = JsConfig.PropertyConvention == PropertyConvention.Lenient ? ParseUtils.LenientPropertyNameResolver : ParseUtils.DefaultPropertyNameResolver; var strTypeLength = strType.Length; while (index < strTypeLength) { var propertyName = JsonTypeSerializer.ParseJsonString(strType, ref index); //Serializer.EatMapKeySeperator(strType, ref index); for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) { break; } } //Whitespace inline if (strType.Length != index) { index++; } var propertyValueStr = Serializer.EatValue(strType, ref index); var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1; //if we already have an instance don't check type info, because then we will have a half deserialized object //we could throw here or just use the existing instance. if (instance == null && possibleTypeInfo && propertyName == JsWriter.TypeAttr) { var explicitTypeName = Serializer.ParseString(propertyValueStr); var explicitType = JsConfig.TypeFinder(explicitTypeName); if (explicitType != null && !explicitType.IsInterface() && !explicitType.IsAbstract()) { instance = explicitType.CreateInstance(); } if (instance == null) { Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr); } else { //If __type info doesn't match, ignore it. if (!type.InstanceOfType(instance)) { instance = null; } else { var derivedType = instance.GetType(); if (derivedType != type) { var derivedTypeConfig = new TypeConfig(derivedType); var map = DeserializeTypeRef.GetTypeAccessorMap(derivedTypeConfig, Serializer); if (map != null) { typeAccessorMap = map; } } } } Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); continue; } if (instance == null) { instance = ctorFn(); } var typeAccessor = propertyResolver.GetTypeAccessorForProperty(propertyName, typeAccessorMap); var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null; if (propType != null) { try { if (typeAccessor != null) { //var parseFn = Serializer.GetParseFn(propType); var parseFn = JsonReader.GetParseFn(propType); var propertyValue = parseFn(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName, propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) { break; } } //Whitespace inline if (index != strType.Length) { var success = strType[index] == JsWriter.ItemSeperator || strType[index] == JsWriter.MapEndChar; index++; if (success) { for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) { break; } } //Whitespace inline } } continue; } catch (Exception e) { if (JsConfig.ThrowOnDeserializationError) { throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr); } } } if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null) { try { var propertyValue = typeAccessor.GetProperty(propertyValueStr); if (typeConfig.OnDeserializing != null) { propertyValue = typeConfig.OnDeserializing(instance, propertyName, propertyValue); } typeAccessor.SetProperty(instance, propertyValue); } catch (Exception e) { if (JsConfig.ThrowOnDeserializationError) { throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, typeAccessor.PropertyType, e); } else { Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr); } } } else if (typeConfig.OnDeserializing != null) { // the property is not known by the DTO typeConfig.OnDeserializing(instance, propertyName, propertyValueStr); } //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index); for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) { break; } } //Whitespace inline if (index != strType.Length) { var success = strType[index] == JsWriter.ItemSeperator || strType[index] == JsWriter.MapEndChar; index++; if (success) { for (; index < strType.Length; index++) { var c = strType[index]; if (c >= JsonTypeSerializer.WhiteSpaceFlags.Length || !JsonTypeSerializer.WhiteSpaceFlags[c]) { break; } } //Whitespace inline } } } return(instance); }
public static IDictionary <TKey, TValue> ParseDictionary <TKey, TValue>( string value, Type createMapType, ParseStringDelegate parseKeyFn, ParseStringDelegate parseValueFn) { if (value == null) { return(null); } var tryToParseItemsAsDictionaries = JsConfig.ConvertObjectTypesIntoStringDictionary && typeof(TValue) == typeof(object); var tryToParseItemsAsPrimitiveTypes = JsConfig.TryToParsePrimitiveTypeValues && typeof(TValue) == typeof(object); var index = VerifyAndGetStartIndex(value, createMapType); var to = (createMapType == null) ? new Dictionary <TKey, TValue>() : (IDictionary <TKey, TValue>)createMapType.CreateInstance(); if (JsonTypeSerializer.IsEmptyMap(value, index)) { return(to); } var valueLength = value.Length; while (index < valueLength) { var keyValue = Serializer.EatMapKey(value, ref index); Serializer.EatMapKeySeperator(value, ref index); var elementStartIndex = index; var elementValue = Serializer.EatTypeValue(value, ref index); if (keyValue == null) { continue; } TKey mapKey = (TKey)parseKeyFn(keyValue); if (tryToParseItemsAsDictionaries) { Serializer.EatWhitespace(value, ref elementStartIndex); if (elementStartIndex < valueLength && value[elementStartIndex] == JsWriter.MapStartChar) { var tmpMap = ParseDictionary <TKey, TValue>(elementValue, createMapType, parseKeyFn, parseValueFn); if (tmpMap != null && tmpMap.Count > 0) { to[mapKey] = (TValue)tmpMap; } } else if (elementStartIndex < valueLength && value[elementStartIndex] == JsWriter.ListStartChar) { to[mapKey] = (TValue)DeserializeList <List <object>, TSerializer> .Parse(elementValue); } else { to[mapKey] = (TValue)(tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength ? DeserializeType <TSerializer> .ParsePrimitive(elementValue, value[elementStartIndex]) : parseValueFn(elementValue)); } } else { if (tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength) { Serializer.EatWhitespace(value, ref elementStartIndex); to[mapKey] = (TValue)DeserializeType <TSerializer> .ParsePrimitive(elementValue, value[elementStartIndex]); } else { to[mapKey] = (TValue)parseValueFn(elementValue); } } Serializer.EatItemSeperatorOrMapEndChar(value, ref index); } return(to); }