public static JToken SerializeToJToken(this JsonSerializer serializer, object value)
        {
            Type vt = value != null?value.GetType() : typeof(object);

            var convName = GetTypeConverterName(vt);
            var conv     = serializer.Converters.Where(c => c.GetType().Name == convName || (c.GetType().IsGenericType&& c.GetType().GetGenericArguments()[0] == vt)).FirstOrDefault();

            if (conv == null && ChoJSONConvertersCache.IsInitialized)
            {
                if (ChoJSONConvertersCache.Contains(convName))
                {
                    conv = ChoJSONConvertersCache.Get(convName);
                }
            }

            JToken t = null;

            if (conv == null)
            {
                t = JToken.FromObject(value, serializer);
            }
            else
            {
                t = JToken.Parse(JsonConvert.SerializeObject(value, serializer.Formatting, conv));
            }
            return(t);
        }
        public static object DeserializeObject(this JsonSerializer serializer, JsonReader reader, Type objType)
        {
            var convName = GetTypeConverterName(objType);
            var conv     = serializer.Converters.Where(c => c.GetType().Name == convName || (c.GetType().IsGenericType&& c.GetType().GetGenericArguments()[0] == objType)).FirstOrDefault();

            if (conv == null && ChoJSONConvertersCache.IsInitialized)
            {
                if (ChoJSONConvertersCache.Contains(convName))
                {
                    conv = ChoJSONConvertersCache.Get(convName);
                }
            }

            JToken t = null;

            if (conv == null)
            {
                return(serializer.Deserialize(reader, objType));
            }
            else
            {
                return(JsonConvert.DeserializeObject(JObject.ReadFrom(reader).ToString(), objType, conv));
            }
        }
Beispiel #3
0
        public static JToken SerializeToJToken(this JsonSerializer serializer, object value, Formatting?formatting = null, JsonSerializerSettings settings = null,
                                               bool dontUseConverter = false, bool enableXmlAttributePrefix = false)
        {
            JsonConverter conv = null;

            if (!dontUseConverter)
            {
                Type vt = value != null?value.GetType() : typeof(object);

                var convName = GetTypeConverterName(vt);
                conv = serializer.Converters.Where(c => c.GetType().Name == convName || (c.GetType().IsGenericType&& c.GetType().GetGenericArguments()[0] == vt)).FirstOrDefault();
                if (conv == null && ChoJSONConvertersCache.IsInitialized)
                {
                    if (ChoJSONConvertersCache.Contains(convName))
                    {
                        conv = ChoJSONConvertersCache.Get(convName);
                    }
                    else if (ChoJSONConvertersCache.Contains(vt))
                    {
                        conv = ChoJSONConvertersCache.Get(vt);
                    }
                }
            }

            if (value != null)
            {
                if (!value.GetType().IsSimple())
                {
                    bool disableImplcityOp = false;
                    if (ChoTypeDescriptor.GetTypeAttribute <ChoTurnOffImplicitOpsAttribute>(value.GetType()) != null)
                    {
                        disableImplcityOp = ChoTypeDescriptor.GetTypeAttribute <ChoTurnOffImplicitOpsAttribute>(value.GetType()).Flag;
                    }

                    if (!disableImplcityOp)
                    {
                        Type to = null;
                        if (value.GetType().CanCastToPrimitiveType(out to))
                        {
                            value = ChoConvert.ConvertTo(value, to);
                        }
                        else if (value.GetType().GetImplicitTypeCastBackOps().Any())
                        {
                            var castTypes = value.GetType().GetImplicitTypeCastBackOps();

                            foreach (var ct in castTypes)
                            {
                                try
                                {
                                    value = ChoConvert.ConvertTo(value, ct);
                                    break;
                                }
                                catch { }
                            }
                        }
                    }
                }
            }

            JToken t = null;

            if (settings != null)
            {
                if (conv != null)
                {
                    settings.Converters.Add(conv);
                }
            }
            if (formatting == null)
            {
                formatting = serializer.Formatting;
            }

            if (settings != null && settings.Context.Context == null && enableXmlAttributePrefix)
            {
                settings.Context = new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.All, new ChoDynamicObject());
                dynamic ctx = settings.Context.Context;
                ctx.EnableXmlAttributePrefix = enableXmlAttributePrefix;
            }

            if (conv != null)
            {
                t = JToken.Parse(JsonConvert.SerializeObject(value, formatting.Value, conv));
            }
            else if (settings != null)
            {
                t = JToken.Parse(JsonConvert.SerializeObject(value, formatting.Value, settings));
            }
            else
            {
                t = JToken.FromObject(value, serializer);
            }
            return(t);
        }