コード例 #1
0
        private static ProposalCollection ReadAllProposals(JsonReader reader, JsonSerializer serializer)
        {
            var res = new ProposalCollection();

            while (reader.TokenType != JsonToken.EndArray)
            {
                var p = serializer.Deserialize <Proposal>(reader);
                res.Add(p);
                reader.Read(); // end object
            }
            return(res);
        }
コード例 #2
0
        public override object ReadJson(JsonReader reader,
                                        Type objectType,
                                        object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.StartObject)
            {
                var proposalCollection = JObject.Load(reader);
                var proposals          = proposalCollection.GetValue("Proposals");

                var res = new ProposalCollection();
                foreach (var proposal in proposals)
                {
                    var p = new Proposal();

                    var propObj = (JObject)proposal;
                    var valName = propObj.GetValue("Name") as JValue;
                    if (valName != null)
                    {
                        var name = valName.Value as string;
                        if (name != null)
                        {
                            p.Name = name.Deserialize <IName>();
                        }
                    }
                    else
                    {
                        p.Name = Names.UnknownGeneral;
                    }
                    var valRelevance = propObj.GetValue("Relevance") as JValue;
                    if (valRelevance != null)
                    {
                        var relevance = (long)valRelevance.Value;
                        p.Relevance = unchecked ((int)relevance);
                    }

                    res.Add(p);
                }
                return(res);
            }
            if (reader.TokenType == JsonToken.StartArray)
            {
                return(new ProposalCollection(serializer.Deserialize <IEnumerable <Proposal> >(reader)));
            }
            throw new JsonSerializationException("expected either array or object to deserialize proposal collection");
        }