Exemplo n.º 1
0
        public static void When_read_simple_object_negative()
        {
            // Read
            using (var tr = new StringReader(s_jsonObject))
                using (var jr = new JsonTextReader(tr))
                {
                    string name   = null;
                    string last   = "smith";
                    string middle = "a";
                    bool   alive  = false;

                    Assert.Throws <JsonReaderException>
                    (
                        () => jr.ReadObject(n =>
                    {
                        switch (n)
                        {
                        case "name": name = (string)jr.Value; return(true);

                        case "last": last = (string)jr.Value; return(true);

                        case "middle": middle = (string)jr.Value; return(true);

                        case "alive": alive = jr.AsBool(); return(true);

                            // Neglect to process all other properties
                        }

                        return(false);
                    })
                    );
                }
        }
Exemplo n.º 2
0
        public static void When_read_simple_object()
        {
            // Read
            using (var tr = new StringReader(s_jsonObject))
                using (var jr = new JsonTextReader(tr))
                {
                    string name   = null;
                    string last   = "smith";
                    string middle = "a";
                    bool   alive  = false;
                    int    age    = -1;
                    var    type   = new System.Data.SqlDbType?[3];
                    Guid?  guid   = null;
                    Uri    uri    = null;
                    byte   u8     = 0;
                    sbyte  s8     = 0;
                    ushort u16    = 0;
                    short  s16    = 0;
                    uint   u32    = 0;
                    int    s32    = 0;
                    long   s64    = 0;

                    jr.ReadObject(n =>
                    {
                        switch (n)
                        {
                        case "name": name = (string)jr.Value; return(true);

                        case "last": last = (string)jr.Value; return(true);

                        case "middle": middle = (string)jr.Value; return(true);

                        case "alive": alive = jr.AsBool(); alive = jr.AsBoolNullable().Value; return(true);

                        case "age": age = jr.AsInt32(); return(true);

                        case "type1": type[0] = jr.AsEnum <System.Data.SqlDbType>(true); return(true);

                        case "type2": type[1] = jr.AsEnumNullable <System.Data.SqlDbType>(true); return(true);

                        case "type3": type[2] = jr.AsEnumNullable <System.Data.SqlDbType>(true); return(true);

                        case "object": jr.SkipObject(); return(true);

                        case "array": jr.SkipCountArray(); return(true);

                        case "guid": guid = jr.AsGuid(); guid = jr.AsGuidNullable(); return(true);

                        case "uri": uri = jr.AsUri(UriKind.Absolute); uri = jr.AsUriNullable(UriKind.Absolute); return(true);

                        case "byte": u8 = jr.AsByte(); u8 = jr.AsByteNullable().Value; return(true);

                        case "sbyte": s8 = jr.AsSByte(); s8 = jr.AsSByteNullable().Value; return(true);

                        case "ushort": u16 = jr.AsUInt16(); u16 = jr.AsUInt16Nullable().Value; return(true);

                        case "short": s16 = jr.AsInt16(); s16 = jr.AsInt16Nullable().Value; return(true);

                        case "uint": u32 = jr.AsUInt32(); u32 = jr.AsUInt32Nullable().Value; return(true);

                        case "int": s32 = jr.AsInt32(); s32 = jr.AsInt32Nullable().Value; return(true);

                        case "long": s64 = jr.AsInt64(); s64 = jr.AsInt64Nullable().Value; return(true);
                        }

                        return(false);
                    });

                    string text = $"{name} {age}";

                    Assert.Equal("joe 99", text);
                    Assert.Equal("joe", name);
                    Assert.Null(last);
                    Assert.Equal(string.Empty, middle);
                    Assert.True(alive);
                    Assert.Equal(99, age);
                    Assert.Equal(System.Data.SqlDbType.TinyInt, type[0]);
                    Assert.Null(type[1]);
                    Assert.Null(type[2]);

                    Assert.Equal(s_guid, guid.Value);
                    Assert.Equal(s_uri.ToString(), uri?.ToString(), StringComparer.Ordinal);

                    Assert.Equal(byte.MaxValue, u8);
                    Assert.Equal(sbyte.MinValue, s8);

                    Assert.Equal(ushort.MaxValue, u16);
                    Assert.Equal(short.MinValue, s16);

                    Assert.Equal(uint.MaxValue, u32);
                    Assert.Equal(int.MinValue, s32);

                    Assert.Equal(long.MinValue, s64);
                }

            // Skip
            using (var tr = new StringReader(s_jsonObject))
                using (var jr = new JsonTextReader(tr))
                {
                    int actualCount = jr.SkipObject();

                    Assert.True(jr.TokenType == JsonToken.EndObject);
                    Assert.Equal(19, actualCount);
                }
        }