/// <summary> /// Creates a <see cref="System.Json.JsonValue"/> object based on an arbitrary CLR object. /// </summary> /// <param name="value">The object to be converted to <see cref="System.Json.JsonValue"/>.</param> /// <returns>The <see cref="System.Json.JsonValue"/> which represents the given object.</returns> /// <remarks>The conversion is done through the <see cref="System.Runtime.Serialization.Json.DataContractJsonSerializer"/>; /// the object is first serialized into JSON using the serializer, then parsed into a <see cref="System.Json.JsonValue"/> /// object.</remarks> public static JsonValue CreateFrom(object value) { JsonValue jsonValue = null; if (value != null) { jsonValue = value as JsonValue; if (jsonValue == null) { jsonValue = JsonValueExtensions.CreatePrimitive(value); if (jsonValue == null) { jsonValue = JsonValueExtensions.CreateFromDynamic(value); if (jsonValue == null) { jsonValue = JsonValueExtensions.CreateFromComplex(value); } } } } return(jsonValue); }
public static bool TryReadAsType(this JsonValue jsonValue, Type type, out object value) { if (jsonValue == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("jsonValue")); } if (type == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type")); } if (type == typeof(JsonValue) || type == typeof(object)) { value = jsonValue; return(true); } if (type == typeof(object[]) || type == typeof(Dictionary <string, object>)) { if (!JsonValueExtensions.CanConvertToClrCollection(jsonValue, type)) { value = null; return(false); } else { value = JsonValueExtensions.ToClrCollection(jsonValue, type); return(true); } } if (jsonValue.TryReadAs(type, out value)) { return(true); } try { using (MemoryStream ms = new MemoryStream()) { jsonValue.Save(ms); ms.Position = 0; DataContractJsonSerializer dcjs = new DataContractJsonSerializer(type); value = dcjs.ReadObject(ms); } return(true); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } value = null; return(false); } }
/// <summary> /// Converts this instance to a collection containing <see cref="object"/> type instances corresponding to the underlying /// elements of this instance. /// </summary> /// <param name="jsonValue">The <see cref="JsonValue"/> instance to convert to the object collection.</param> /// <param name="type">The <see cref="Type"/> of the collection to convert this instance to.</param> /// <returns>An object representing a CLR collection depending on the <see cref="JsonType"/> value of this instance and the specified type value.</returns> private static object ToClrCollection(JsonValue jsonValue, Type type) { object collection = null; if (CanConvertToClrCollection(jsonValue, type)) { JsonValue parentValue = jsonValue; Queue <KeyValuePair <string, JsonValue> > childValues = null; Stack <ToClrCollectionStackInfo> stackInfo = new Stack <ToClrCollectionStackInfo>(); int currentIndex = 0; collection = JsonValueExtensions.CreateClrCollection(parentValue); do { if (childValues == null) { childValues = new Queue <KeyValuePair <string, JsonValue> >(parentValue); } while (childValues != null && childValues.Count > 0) { KeyValuePair <string, JsonValue> item = childValues.Dequeue(); JsonValue childValue = item.Value; switch (childValue.JsonType) { case JsonType.Array: case JsonType.Object: object childCollection = JsonValueExtensions.CreateClrCollection(childValue); InsertClrItem(collection, ref currentIndex, item.Key, childCollection); stackInfo.Push(new ToClrCollectionStackInfo(parentValue, collection, currentIndex, childValues)); parentValue = item.Value; childValues = null; collection = childCollection; currentIndex = 0; break; default: InsertClrItem(collection, ref currentIndex, item.Key, item.Value.Read()); break; } } if (childValues != null && stackInfo.Count > 0) { ToClrCollectionStackInfo info = stackInfo.Pop(); collection = info.Collection; childValues = info.JsonValueChildren; parentValue = info.ParentJsonValue; currentIndex = info.CurrentIndex; } }while (stackInfo.Count > 0 || childValues == null || childValues.Count > 0); } return(collection); }
public static T ReadAsType <T>(this JsonValue jsonValue) { if (jsonValue == null) { throw new ArgumentNullException("jsonValue"); } return((T)JsonValueExtensions.ReadAsType(jsonValue, typeof(T))); }
/// <summary> /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T. /// </summary> /// <typeparam name="T">The type to which the conversion is being performed.</typeparam> /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param> /// <returns>An instance of T initialized with the <see cref="System.Json.JsonValue"/> value /// specified if the conversion.</returns> /// <exception cref="System.NotSupportedException">If this <see cref="System.Json.JsonValue"/> value cannot be /// converted into the type T.</exception> public static T ReadAsType <T>(this JsonValue jsonValue) { if (jsonValue == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("jsonValue")); } return((T)JsonValueExtensions.ReadAsType(jsonValue, typeof(T))); }
/// <summary> /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T, returning a fallback value /// if the conversion fails. /// </summary> /// <typeparam name="T">The type to which the conversion is being performed.</typeparam> /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param> /// <param name="fallback">A fallback value to be retuned in case the conversion cannot be performed.</param> /// <returns>An instance of T initialized with the <see cref="System.Json.JsonValue"/> value /// specified if the conversion succeeds or the specified fallback value if it fails.</returns> public static T ReadAsType <T>(this JsonValue jsonValue, T fallback) { if (jsonValue == null) { throw new ArgumentNullException("jsonValue"); } T outVal; if (JsonValueExtensions.TryReadAsType <T>(jsonValue, out outVal)) { return(outVal); } return(fallback); }
/// <summary> /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T, returning a fallback value /// if the conversion fails. /// </summary> /// <typeparam name="T">The type to which the conversion is being performed.</typeparam> /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param> /// <param name="fallback">A fallback value to be retuned in case the conversion cannot be performed.</param> /// <returns>An instance of T initialized with the <see cref="System.Json.JsonValue"/> value /// specified if the conversion succeeds or the specified fallback value if it fails.</returns> public static T ReadAsType <T>(this JsonValue jsonValue, T fallback) { if (jsonValue == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("jsonValue")); } T outVal; if (JsonValueExtensions.TryReadAsType <T>(jsonValue, out outVal)) { return(outVal); } return(fallback); }
/// <summary> /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T. /// </summary> /// <typeparam name="T">The type to which the conversion is being performed.</typeparam> /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param> /// <param name="valueOfT">An instance of T initialized with this instance, or the default /// value of T, if the conversion cannot be performed.</param> /// <returns>true if this <see cref="System.Json.JsonValue"/> instance can be read as type T; otherwise, false.</returns> public static bool TryReadAsType <T>(this JsonValue jsonValue, out T valueOfT) { if (jsonValue == null) { throw new ArgumentNullException("jsonValue"); } object value; if (JsonValueExtensions.TryReadAsType(jsonValue, typeof(T), out value)) { valueOfT = (T)value; return(true); } valueOfT = default(T); return(false); }
/// <summary> /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into an instance of the specified type. /// </summary> /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param> /// <param name="type">The type to which the conversion is being performed.</param> /// <returns>An object instance initialized with the <see cref="System.Json.JsonValue"/> value /// specified if the conversion.</returns> /// <exception cref="System.NotSupportedException">If this <see cref="System.Json.JsonValue"/> value cannot be /// converted into the type T.</exception> public static object ReadAsType(this JsonValue jsonValue, Type type) { if (jsonValue == null) { throw new ArgumentNullException("jsonValue"); } if (type == null) { throw new ArgumentNullException("type"); } object result; if (JsonValueExtensions.TryReadAsType(jsonValue, type, out result)) { return(result); } throw new NotSupportedException(RS.Format(System.Json.Properties.Resources.CannotReadAsType, jsonValue.GetType().FullName, type.FullName)); }
/// <summary> /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into an instance of the specified type. /// </summary> /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param> /// <param name="type">The type to which the conversion is being performed.</param> /// <returns>An object instance initialized with the <see cref="System.Json.JsonValue"/> value /// specified if the conversion.</returns> /// <exception cref="System.NotSupportedException">If this <see cref="System.Json.JsonValue"/> value cannot be /// converted into the type T.</exception> public static object ReadAsType(this JsonValue jsonValue, Type type) { if (jsonValue == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("jsonValue")); } if (type == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type")); } object result; if (JsonValueExtensions.TryReadAsType(jsonValue, type, out result)) { return(result); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SG.GetString(SR.CannotReadAsType, jsonValue.GetType().FullName, type.FullName))); }
private static JsonValue CreateFromDynamic(object value) { JsonObject parent = null; DynamicObject dynObj = value as DynamicObject; if (dynObj != null) { parent = new JsonObject(); Stack <CreateFromTypeStackInfo> infoStack = new Stack <CreateFromTypeStackInfo>(); IEnumerator <string> keys = null; do { if (keys == null) { keys = dynObj.GetDynamicMemberNames().GetEnumerator(); } while (keys.MoveNext()) { JsonValue child = null; string key = keys.Current; SimpleGetMemberBinder binder = new SimpleGetMemberBinder(key); if (dynObj.TryGetMember(binder, out value)) { DynamicObject childDynObj = value as DynamicObject; if (childDynObj != null) { child = new JsonObject(); parent.Add(key, child); infoStack.Push(new CreateFromTypeStackInfo(parent, dynObj, keys)); parent = child as JsonObject; dynObj = childDynObj; keys = null; break; } else { if (value != null) { child = value as JsonValue; if (child == null) { child = JsonValueExtensions.CreatePrimitive(value); if (child == null) { child = JsonValueExtensions.CreateFromComplex(value); } } } parent.Add(key, child); } } } if (infoStack.Count > 0 && keys != null) { CreateFromTypeStackInfo info = infoStack.Pop(); parent = info.JsonObject; dynObj = info.DynamicObject; keys = info.Keys; } }while (infoStack.Count > 0); } return(parent); }