Ejemplo n.º 1
0
        // Token: 0x0600149F RID: 5279 RVA: 0x0006D630 File Offset: 0x0006B830
        private static DiscriminatedUnionConverter.Union CreateUnion(Type t)
        {
            MethodCall <object, object> preComputeUnionTagReader = FSharpUtils.Instance.PreComputeUnionTagReader;
            object target = null;

            object[] array = new object[2];
            array[0] = t;
            DiscriminatedUnionConverter.Union union         = new DiscriminatedUnionConverter.Union((FSharpFunction)preComputeUnionTagReader(target, array), new List <DiscriminatedUnionConverter.UnionCase>());
            MethodCall <object, object>       getUnionCases = FSharpUtils.Instance.GetUnionCases;
            object target2 = null;

            object[] array2 = new object[2];
            array2[0] = t;
            foreach (object obj in (object[])getUnionCases(target2, array2))
            {
                int            tag    = (int)FSharpUtils.Instance.GetUnionCaseInfoTag(obj);
                string         name   = (string)FSharpUtils.Instance.GetUnionCaseInfoName(obj);
                PropertyInfo[] fields = (PropertyInfo[])FSharpUtils.Instance.GetUnionCaseInfoFields(obj, new object[0]);
                MethodCall <object, object> preComputeUnionReader = FSharpUtils.Instance.PreComputeUnionReader;
                object   target3 = null;
                object[] array4  = new object[2];
                array4[0] = obj;
                FSharpFunction fieldReader = (FSharpFunction)preComputeUnionReader(target3, array4);
                MethodCall <object, object> preComputeUnionConstructor = FSharpUtils.Instance.PreComputeUnionConstructor;
                object   target4 = null;
                object[] array5  = new object[2];
                array5[0] = obj;
                DiscriminatedUnionConverter.UnionCase item = new DiscriminatedUnionConverter.UnionCase(tag, name, fields, fieldReader, (FSharpFunction)preComputeUnionConstructor(target4, array5));
                union.Cases.Add(item);
            }
            return(union);
        }
Ejemplo n.º 2
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            DefaultContractResolver contractResolver = serializer.ContractResolver as DefaultContractResolver;
            Type type = DiscriminatedUnionConverter.UnionTypeLookupCache.Get(value.GetType());

            DiscriminatedUnionConverter.Union union = DiscriminatedUnionConverter.UnionCache.Get(type);
            int num = (int)union.TagReader.Invoke(new object[] { value });

            DiscriminatedUnionConverter.UnionCase unionCase = union.Cases.Single <DiscriminatedUnionConverter.UnionCase>((DiscriminatedUnionConverter.UnionCase c) => c.Tag == num);
            writer.WriteStartObject();
            writer.WritePropertyName((contractResolver != null ? contractResolver.GetResolvedPropertyName("Case") : "Case"));
            writer.WriteValue(unionCase.Name);
            if (unionCase.Fields != null && unionCase.Fields.Length != 0)
            {
                object[] objArray = (object[])unionCase.FieldReader.Invoke(new object[] { value });
                writer.WritePropertyName((contractResolver != null ? contractResolver.GetResolvedPropertyName("Fields") : "Fields"));
                writer.WriteStartArray();
                object[] objArray1 = objArray;
                for (int i = 0; i < (int)objArray1.Length; i++)
                {
                    serializer.Serialize(writer, objArray1[i]);
                }
                writer.WriteEndArray();
            }
            writer.WriteEndObject();
        }
Ejemplo n.º 3
0
 private static DiscriminatedUnionConverter.Union CreateUnion(Type t)
 {
     DiscriminatedUnionConverter.Union union = new DiscriminatedUnionConverter.Union()
     {
         TagReader = (FSharpFunction)FSharpUtils.PreComputeUnionTagReader(null, new object[] { t, null }),
         Cases     = new List <DiscriminatedUnionConverter.UnionCase>()
     };
     object[] getUnionCases = (object[])FSharpUtils.GetUnionCases(null, new object[] { t, null });
     for (int i = 0; i < (int)getUnionCases.Length; i++)
     {
         object obj = getUnionCases[i];
         DiscriminatedUnionConverter.UnionCase unionCase = new DiscriminatedUnionConverter.UnionCase()
         {
             Tag         = (int)FSharpUtils.GetUnionCaseInfoTag(obj),
             Name        = (string)FSharpUtils.GetUnionCaseInfoName(obj),
             Fields      = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(obj, new object[0]),
             FieldReader = (FSharpFunction)FSharpUtils.PreComputeUnionReader(null, new object[] { obj, null }),
             Constructor = (FSharpFunction)FSharpUtils.PreComputeUnionConstructor(null, new object[] { obj, null })
         };
         union.Cases.Add(unionCase);
     }
     return(union);
 }
Ejemplo n.º 4
0
        // Token: 0x060014A0 RID: 5280 RVA: 0x0006D73C File Offset: 0x0006B93C
        public override void WriteJson(JsonWriter writer, [Nullable(2)] object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            DefaultContractResolver defaultContractResolver = serializer.ContractResolver as DefaultContractResolver;
            Type key = DiscriminatedUnionConverter.UnionTypeLookupCache.Get(value.GetType());

            DiscriminatedUnionConverter.Union union = DiscriminatedUnionConverter.UnionCache.Get(key);
            int tag = (int)union.TagReader.Invoke(new object[]
            {
                value
            });

            DiscriminatedUnionConverter.UnionCase unionCase = union.Cases.Single((DiscriminatedUnionConverter.UnionCase c) => c.Tag == tag);
            writer.WriteStartObject();
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Case") : "Case");
            writer.WriteValue(unionCase.Name);
            if (unionCase.Fields != null && unionCase.Fields.Length != 0)
            {
                object[] array = (object[])unionCase.FieldReader.Invoke(new object[]
                {
                    value
                });
                writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Fields") : "Fields");
                writer.WriteStartArray();
                foreach (object value2 in array)
                {
                    serializer.Serialize(writer, value2);
                }
                writer.WriteEndArray();
            }
            writer.WriteEndObject();
        }
Ejemplo n.º 5
0
        public override object ReadJson(JsonReader reader, Type objectType, [Nullable(2)] object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            DiscriminatedUnionConverter.UnionCase unionCase = null;

            string caseName = null;
            JArray jarray   = null;

            reader.ReadAndAssert();
            Func <DiscriminatedUnionConverter.UnionCase, bool> < > 9__0;
            while (reader.TokenType == JsonToken.PropertyName)
            {
                string text = reader.Value.ToString();
                if (string.Equals(text, "Case", StringComparison.OrdinalIgnoreCase))
                {
                    reader.ReadAndAssert();
                    DiscriminatedUnionConverter.Union union = DiscriminatedUnionConverter.UnionCache.Get(objectType);
                    caseName = reader.Value.ToString();
                    IEnumerable <DiscriminatedUnionConverter.UnionCase> cases = union.Cases;
                    Func <DiscriminatedUnionConverter.UnionCase, bool>  predicate;
                    if ((predicate = < > 9__0) == null)
                    {
                        predicate = (< > 9__0 = ((DiscriminatedUnionConverter.UnionCase c) => c.Name == caseName));
                    }
                    unionCase = cases.SingleOrDefault(predicate);
                    if (unionCase == null)
                    {
                        throw JsonSerializationException.Create(reader, "No union type found with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, caseName));
                    }
                }
                else
                {
                    if (!string.Equals(text, "Fields", StringComparison.OrdinalIgnoreCase))
                    {
                        throw JsonSerializationException.Create(reader, "Unexpected property '{0}' found when reading union.".FormatWith(CultureInfo.InvariantCulture, text));
                    }
                    reader.ReadAndAssert();
                    if (reader.TokenType != JsonToken.StartArray)
                    {
                        throw JsonSerializationException.Create(reader, "Union fields must been an array.");
                    }
                    jarray = (JArray)JToken.ReadFrom(reader);
                }
                reader.ReadAndAssert();
            }
            if (unionCase == null)
            {
                throw JsonSerializationException.Create(reader, "No '{0}' property with union name found.".FormatWith(CultureInfo.InvariantCulture, "Case"));
            }
            object[] array = new object[unionCase.Fields.Length];
            if (unionCase.Fields.Length != 0 && jarray == null)
            {
                throw JsonSerializationException.Create(reader, "No '{0}' property with union fields found.".FormatWith(CultureInfo.InvariantCulture, "Fields"));
            }
            if (jarray != null)
            {
                if (unionCase.Fields.Length != jarray.Count)
                {
                    throw JsonSerializationException.Create(reader, "The number of field values does not match the number of properties defined by union '{0}'.".FormatWith(CultureInfo.InvariantCulture, caseName));
                }
                for (int i = 0; i < jarray.Count; i++)
                {
                    JToken       jtoken       = jarray[i];
                    PropertyInfo propertyInfo = unionCase.Fields[i];
                    array[i] = jtoken.ToObject(propertyInfo.PropertyType, serializer);
                }
            }
            object[] args = new object[]
            {
                array
            };
            return(unionCase.Constructor.Invoke(args));
        }
Ejemplo n.º 6
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Func <DiscriminatedUnionConverter.UnionCase, bool> func = null;

            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            DiscriminatedUnionConverter.UnionCase unionCase = null;
            string str     = null;
            JArray jArrays = null;

            reader.ReadAndAssert();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                string str1 = reader.Value.ToString();
                if (!string.Equals(str1, "Case", StringComparison.OrdinalIgnoreCase))
                {
                    if (!string.Equals(str1, "Fields", StringComparison.OrdinalIgnoreCase))
                    {
                        throw JsonSerializationException.Create(reader, "Unexpected property '{0}' found when reading union.".FormatWith(CultureInfo.InvariantCulture, str1));
                    }
                    reader.ReadAndAssert();
                    if (reader.TokenType != JsonToken.StartArray)
                    {
                        throw JsonSerializationException.Create(reader, "Union fields must been an array.");
                    }
                    jArrays = (JArray)JToken.ReadFrom(reader);
                }
                else
                {
                    reader.ReadAndAssert();
                    DiscriminatedUnionConverter.Union union = DiscriminatedUnionConverter.UnionCache.Get(objectType);
                    str = reader.Value.ToString();
                    List <DiscriminatedUnionConverter.UnionCase>       cases = union.Cases;
                    Func <DiscriminatedUnionConverter.UnionCase, bool> func1 = func;
                    if (func1 == null)
                    {
                        Func <DiscriminatedUnionConverter.UnionCase, bool> name  = (DiscriminatedUnionConverter.UnionCase c) => c.Name == str;
                        Func <DiscriminatedUnionConverter.UnionCase, bool> func2 = name;
                        func  = name;
                        func1 = func2;
                    }
                    unionCase = cases.SingleOrDefault <DiscriminatedUnionConverter.UnionCase>(func1);
                    if (unionCase == null)
                    {
                        throw JsonSerializationException.Create(reader, "No union type found with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, str));
                    }
                }
                reader.ReadAndAssert();
            }
            if (unionCase == null)
            {
                throw JsonSerializationException.Create(reader, "No '{0}' property with union name found.".FormatWith(CultureInfo.InvariantCulture, "Case"));
            }
            object[] obj = new object[(int)unionCase.Fields.Length];
            if (unionCase.Fields.Length != 0 && jArrays == null)
            {
                throw JsonSerializationException.Create(reader, "No '{0}' property with union fields found.".FormatWith(CultureInfo.InvariantCulture, "Fields"));
            }
            if (jArrays != null)
            {
                if ((int)unionCase.Fields.Length != jArrays.Count)
                {
                    throw JsonSerializationException.Create(reader, "The number of field values does not match the number of properties defined by union '{0}'.".FormatWith(CultureInfo.InvariantCulture, str));
                }
                for (int i = 0; i < jArrays.Count; i++)
                {
                    JToken       item   = jArrays[i];
                    PropertyInfo fields = unionCase.Fields[i];
                    obj[i] = item.ToObject(fields.PropertyType, serializer);
                }
            }
            object[] objArray = new object[] { obj };
            return(unionCase.Constructor.Invoke(objArray));
        }