當前位置: 首頁>>代碼示例>>C#>>正文


C# JsonReader.Assert方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.JsonReader.Assert方法的典型用法代碼示例。如果您正苦於以下問題:C# JsonReader.Assert方法的具體用法?C# JsonReader.Assert怎麽用?C# JsonReader.Assert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.JsonReader的用法示例。


在下文中一共展示了JsonReader.Assert方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;

            reader.Assert(JsonToken.StartObject);

            string toString = null;
            string idObj = null;
            string typeStr = null;
            Entity entity = null;

            reader.Read();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                switch ((string)reader.Value)
                {
                    case "toStr": toString = reader.ReadAsString(); break;
                    case "id": idObj = reader.ReadAsString(); break;
                    case "EntityType": typeStr = reader.ReadAsString(); break;
                    case "entity":
                        reader.Read();
                        entity = (Entity)serializer.Deserialize(reader, typeof(Entity));
                        break;
                    default: throw new InvalidOperationException("unexpected property " + (string)reader.Value);
                }

                reader.Read();
            }

            reader.Assert(JsonToken.EndObject);

            Type type = TypeLogic.GetType(typeStr);

            PrimaryKey? idOrNull = idObj == null ? (PrimaryKey?)null : PrimaryKey.Parse(idObj, type);

            if (entity == null)
                return Lite.Create(type, idOrNull.Value, toString);

            var result = entity.ToLiteFat(toString);

            if (result.EntityType != type)
                throw new InvalidOperationException("Types don't match");

            if (result.IdOrNull != idOrNull)
                throw new InvalidOperationException("Id's don't match");

            var existing = existingValue as Lite<Entity>;

            if (existing.Is(result) && existing.EntityOrNull == null && result.EntityOrNull != null)
            {
                existing.SetEntity(result.EntityOrNull);
                return existing;
            }

           return result;
        }
開發者ID:signumsoftware,項目名稱:framework,代碼行數:57,代碼來源:LiteJsonConverter.cs

示例2: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;

            using (EntityCache ec = new EntityCache())
            {
                reader.Assert(JsonToken.StartObject);

                bool markedAsModified;
                ModifiableEntity mod = GetEntity(reader, objectType, existingValue, serializer, out markedAsModified);
                
                var pr = JsonSerializerExtensions.CurrentPropertyRoute;
                if (pr == null || mod is IRootEntity)
                    pr = PropertyRoute.Root(mod.GetType());
                else if (pr.Type.ElementType() == objectType)
                    pr = pr.Add("Item"); //Because we have a custom MListJsonConverter but not for other simpler collections

                var dic = PropertyConverter.GetPropertyConverters(mod.GetType());

                while (reader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)reader.Value == "mixins")
                    {
                        var entity = (Entity)mod;
                        reader.Read();
                        reader.Assert(JsonToken.StartObject);

                        reader.Read();
                        while (reader.TokenType == JsonToken.PropertyName)
                        {
                            var mixin = entity[(string)reader.Value];

                            reader.Read();

                            using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr.Add(mixin.GetType())))
                                serializer.DeserializeValue(reader, mixin.GetType(), mixin);

                            reader.Read();
                        }

                        reader.Assert(JsonToken.EndObject);
                        reader.Read();
                    }
                    else
                    {
                        PropertyConverter pc = dic.GetOrThrow((string)reader.Value);

                        reader.Read();
                        ReadJsonProperty(reader, serializer, mod, pc, pr, markedAsModified);

                        reader.Read();
                    }
                }

                reader.Assert(JsonToken.EndObject);

                AfterDeserilization.Invoke(mod);

                return mod;
            }
        }
開發者ID:signumsoftware,項目名稱:framework,代碼行數:62,代碼來源:EntityJsonConverter.cs


注:本文中的Newtonsoft.Json.JsonReader.Assert方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。