Example #1
0
        /// <summary>
        /// Deserializes response JSON object.
        /// </summary>
        /// <param name="json">String in JSON format.</param>
        /// <param name="knownTypes">
        /// Types that may be present in the object graph. Can be null.
        /// </param>
        /// <returns>
        /// Deserialized object.
        /// </returns>
        public static T DeserializeResponse <T>(string json,
                                                IEnumerable <Type> knownTypes)
        {
            // validate response string
            if (String.IsNullOrEmpty(json))
            {
                throw new SerializationException(Properties.Messages.Error_InvalidJsonResponseString);
            }

            T obj = default(T);

            // check if response string contains error data
            if (JsonProcHelper.IsFaultResponse(json))
            {
                // deserialize error object
                GPFaultResponse errorObj = Deserialize <GPFaultResponse>(json);

                // create response object
                obj = Activator.CreateInstance <T>();

                // set error data
                IFaultInfo fault = obj as IFaultInfo;
                if (fault != null)
                {
                    fault.FaultInfo = errorObj.Error;
                }
            }
            else
            {
                // deserialize response object
                obj = Deserialize <T>(json, knownTypes, true);
            }

            return(obj);
        }
Example #2
0
        /// <summary>
        /// Deserializes object of the specified type from JSON string.
        /// </summary>
        /// <param name="type">The type of object to be deserialized.</param>
        /// <param name="json">The JSON string containing serialized object.</param>
        /// <param name="knownTypes">Collection of types that may be present in the object
        /// graph.</param>
        /// <param name="doPreProcessing">A value indicating whether pre-processing should be
        /// performed.</param>
        /// <returns>A reference to the deserialized object.</returns>
        public static object Deserialize(
            Type type,
            string json,
            IEnumerable <Type> knownTypes,
            bool doPreProcessing)
        {
            Debug.Assert(type != null);
            Debug.Assert(json != null);

            if (doPreProcessing)
            {
                json = JsonProcHelper.DoPreProcessing(json);
            }

            var serializer = new DataContractJsonSerializer(type, knownTypes);

            return(_Deserialize(serializer, json));
        }
Example #3
0
        /// <summary>
        /// Serializes JSON object.
        /// </summary>
        /// <param name="obj">
        /// Object to serialize.
        /// </param>
        /// <param name="doPostProcessing">
        /// A boolean value indicating whether post-processing should be
        /// performed.
        /// </param>
        /// <param name="knownTypes">
        /// Types that may be present in the object graph. Can be null.
        /// </param>
        /// <returns>
        /// String in JSON format.
        /// </returns>
        public static string Serialize(object obj, IEnumerable <Type> knownTypes,
                                       bool doPostProcessing)
        {
            Debug.Assert(obj != null);

            string json = null;

            if (knownTypes != null)
            {
                json = Serialize(obj, knownTypes);
            }
            else
            {
                json = Serialize(obj);
            }

            // post-processing
            if (doPostProcessing)
            {
                json = JsonProcHelper.DoPostProcessing(json);
            }

            return(json);
        }
Example #4
0
        /// <summary>
        /// Deserializes JSON object.
        /// </summary>
        /// <param name="json">String in JSON format.</param>
        /// <param name="doPreProcessing">
        /// A boolean value indicating whether pre-processing should be
        /// performed.
        /// </param>
        /// <param name="knownTypes">
        /// Types that may be present in the object graph. Can be null.
        /// </param>
        /// <returns>
        /// Deserialized object.
        /// </returns>
        public static T Deserialize <T>(string json, IEnumerable <Type> knownTypes,
                                        bool doPreProcessing)
        {
            Debug.Assert(json != null);

            // pre-processing
            if (doPreProcessing)
            {
                json = JsonProcHelper.DoPreProcessing(json);
            }

            T obj;

            if (knownTypes != null)
            {
                obj = Deserialize <T>(json, knownTypes);
            }
            else
            {
                obj = Deserialize <T>(json);
            }

            return(obj);
        }