コード例 #1
0
        public static bool Parse(byte[] buffer, int start, DtoLikes dto, Storage store)
        {
            JsonReader reader = new JsonReader(buffer, start);

            if (reader.ReadIsNull() || !reader.ReadIsBeginObject())
            {
                return(false);
            }

            if (!reader.ReadPropertyNameSegmentRaw().EqualTo(s_likes))
            {
                return(false);
            }

            // read array members
            if (!reader.ReadIsBeginArray())
            {
                return(false);
            }

            while (true)
            {
                var like = new DtoLike();

                // read array of DtoLike objects
                if (!reader.ReadIsBeginObject())
                {
                    break;
                }

                // like properties
                while (true)
                {
                    var prop = reader.ReadPropertyNameSegmentRaw();
                    if (prop[0] == (byte)'t') // ts
                    {
                        if (reader.GetCurrentJsonToken() != JsonToken.Number)
                        {
                            return(false);
                        }
                        like.ts = Like.ExtToIntTS(reader.ReadInt32());
                    }
                    else
                    if (prop[prop.Count - 1] == (byte)'e') // likee
                    {
                        if (reader.GetCurrentJsonToken() != JsonToken.Number)
                        {
                            return(false);
                        }
                        if (!Mapper.ExtIdToIntId(reader.ReadInt32(), out like.likee))
                        {
                            return(false);
                        }
                    }
                    else
                    if (prop[prop.Count - 1] == (byte)'r') // liker
                    {
                        if (reader.GetCurrentJsonToken() != JsonToken.Number)
                        {
                            return(false);
                        }
                        if (!Mapper.ExtIdToIntId(reader.ReadInt32(), out like.liker))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }

                    if (!reader.ReadIsValueSeparator())
                    {
                        break;
                    }
                }

                if (!reader.ReadIsEndObject())
                {
                    return(false);
                }

                // add the like
                if (like.likee > 0 && like.liker > 0)
                {
                    dto.likes.Add(like);
                }
                else
                {
                    return(false);
                }

                if (!reader.ReadIsValueSeparator())
                {
                    break;
                }
            }
            if (!reader.ReadIsEndArray())
            {
                return(false);
            }
            if (!reader.ReadIsEndObject())
            {
                return(false);
            }

            return(true);
        }
コード例 #2
0
        // using low-level to catch wrong structure or properties
        // ids and likes are internalized
        public static bool Parse(ref JsonReader reader, DtoAccount dto, Storage store)
        {
            var count = 0;

            if (reader.ReadIsNull() || !reader.ReadIsBeginObject())
            {
                return(false);
            }
            while (true)
            {
                var prop = reader.ReadPropertyNameSegmentRaw();
                // id
                if (prop.EqualTo(s_id))
                {
                    if (reader.GetCurrentJsonToken() != JsonToken.Number)
                    {
                        return(false);
                    }
                    dto.id     = Mapper.ExtIdToIntIdCreate(reader.ReadInt32());
                    dto.flags |= DtoFlags.Id;
                }
                else
                // status
                if (prop.EqualTo(s_status))
                {
                    var st = new AString(reader.ReadStringSegmentRaw()).InPlaceUnescape();
                    if (st == s_Free)
                    {
                        dto.status = STATUS_FREE;
                    }
                    else
                    if (st == s_Taken)
                    {
                        dto.status = STATUS_TAKEN;
                    }
                    else
                    if (st == s_Complicated)
                    {
                        dto.status = STATUS_COMPLICATED;
                    }
                    else
                    {
                        return(false);
                    }
                    dto.flags |= DtoFlags.Status;
                }
                else
                // sex
                if (prop.EqualTo(s_sex))
                {
                    var st = reader.ReadStringSegmentRaw();
                    if (st.EqualTo(s_male))
                    {
                        dto.sex = SEX_MALE;
                    }
                    else
                    if (st.EqualTo(s_female))
                    {
                        dto.sex = SEX_FEMALE;
                    }
                    else
                    {
                        return(false);
                    }
                    dto.flags |= DtoFlags.Sex;
                }
                else
                // email
                if (prop.EqualTo(s_email))
                {
                    dto.email  = reader.ReadStringSegmentRaw();
                    dto.flags |= DtoFlags.Email;
                }
                else
                // phone
                if (prop.EqualTo(s_phone))
                {
                    dto.phone  = new AString(reader.ReadStringSegmentRaw()).Duplicate();
                    dto.flags |= DtoFlags.Phone;
                }
                else
                // fname
                if (prop.EqualTo(s_fname))
                {
                    dto.fnameIdx = (short)store.Fnames.GetOrCreateRange(
                        new AString(reader.ReadStringSegmentRaw()).InPlaceUnescape()).Index;
                    dto.flags |= DtoFlags.Fname;
                }
                else
                // sname
                if (prop.EqualTo(s_sname))
                {
                    dto.snameIdx = (short)store.Snames.GetOrCreateRange(
                        new AString(reader.ReadStringSegmentRaw()).InPlaceUnescape()).Index;
                    dto.flags |= DtoFlags.Sname;
                }
                else
                // country
                if (prop.EqualTo(s_country))
                {
                    dto.countryIdx = (byte)store.Countries.GetOrCreateRange(
                        new AString(reader.ReadStringSegmentRaw()).InPlaceUnescape()).Index;
                    dto.flags |= DtoFlags.Country;
                }
                else
                // city
                if (prop.EqualTo(s_city))
                {
                    dto.cityIdx = (short)store.Cities.GetOrCreateRange(
                        new AString(reader.ReadStringSegmentRaw()).InPlaceUnescape()).Index;
                    dto.flags |= DtoFlags.City;
                }
                else
                // birth
                if (prop.EqualTo(s_birth))
                {
                    if (reader.GetCurrentJsonToken() != JsonToken.Number)
                    {
                        return(false);
                    }
                    dto.birth  = reader.ReadInt32();
                    dto.flags |= DtoFlags.Birth;
                }
                else
                // joined
                if (prop.EqualTo(s_joined))
                {
                    if (reader.GetCurrentJsonToken() != JsonToken.Number)
                    {
                        return(false);
                    }
                    dto.joined = reader.ReadInt32();
                    dto.flags |= DtoFlags.Joined;
                }
                else
                // premium
                if (prop.EqualTo(s_premium))
                {
                    if (!DtoPremium.Parse(ref reader, ref dto.premium))
                    {
                        return(false);
                    }
                    dto.flags |= DtoFlags.Premium;
                }
                else
                // interests
                if (prop.EqualTo(s_interests))
                {
                    if (!reader.ReadIsBeginArray())
                    {
                        return(false);
                    }
                    while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count))
                    {
                        dto.interests.Set(store.Interests.GetOrCreateRange(new AString(reader.ReadStringSegmentRaw()).InPlaceUnescape()).Index);
                    }
                    dto.flags |= DtoFlags.Interests;
                }
                else
                // likes
                if (prop.EqualTo(s_likes))
                {
                    if (!reader.ReadIsBeginArray())
                    {
                        return(false);
                    }

                    // read likes
                    if (reader.GetCurrentJsonToken() == JsonToken.BeginObject)
                    {
                        while (true)
                        {
                            Like like = new Like();

                            if (!reader.ReadIsBeginObject())
                            {
                                return(false);
                            }

                            while (true)
                            {
                                var propName = reader.ReadPropertyNameSegmentRaw();
                                if (propName[0] == (byte)'t') // ts
                                {
                                    if (reader.GetCurrentJsonToken() != JsonToken.Number)
                                    {
                                        return(false);
                                    }
                                    like.ts = Like.ExtToIntTS(reader.ReadInt32());
                                }
                                else
                                if (propName[0] == (byte)'i') // id
                                {
                                    if (reader.GetCurrentJsonToken() != JsonToken.Number)
                                    {
                                        return(false);
                                    }
                                    if (!Mapper.ExtIdToIntId(reader.ReadInt32(), out like.id))
                                    {
                                        return(false);
                                    }
                                }
                                else // not ts nor id
                                {
                                    return(false);
                                }

                                if (!reader.ReadIsValueSeparator())
                                {
                                    break;
                                }
                            }

                            if (!reader.ReadIsEndObject())
                            {
                                return(false);
                            }

                            dto.likes.Add(like);
                            if (!reader.ReadIsValueSeparator())
                            {
                                break;
                            }
                        }
                    }

                    if (!reader.ReadIsEndArray())
                    {
                        return(false);
                    }
                    dto.flags |= DtoFlags.Likes;
                }
                else
                {
                    return(false);
                }

                if (!reader.ReadIsValueSeparator())
                {
                    break;
                }
            }
            if (!reader.ReadIsEndObject())
            {
                return(false);
            }

            return(true);
        }