예제 #1
0
        public List <TermInfo> LoadSetTerms(long setID)
        {
            SetInfo set;

            if (!sets.TryGetValue(setID, out set))
            {
                return(null);
            }

            try {
                using (var stream = storage.OpenFile("Set" + setID.ToString(CultureInfo.InvariantCulture), FileMode.Open)) {
                    using (var reader = new BinaryReader(stream)) {
                        int termCount = reader.ReadInt32();

                        var terms = new List <TermInfo>();
                        while (termCount-- > 0)
                        {
                            var term = new TermInfo {
                                ID         = reader.ReadInt64(),
                                Term       = reader.ReadString(),
                                Definition = reader.ReadString(),
                                Image      = reader.ReadString()
                            };

                            if (string.IsNullOrEmpty(term.Image))
                            {
                                term.Image = null;
                            }

                            terms.Add(term);
                        }

                        set.Terms   = terms;
                        sets[setID] = set;
                        return(terms);
                    }
                }
            } catch (IsolatedStorageException) {
                return(null);
            } catch (IOException) {
                return(null);
            }
        }
예제 #2
0
        public SetInfo(JsonValue json, IJsonContext context) : this()
        {
            var dict = json as JsonDictionary;

            if (dict == null)
            {
                throw new JsonConvertException("Expected a JSON dictionary to be converted to a SetInfo");
            }

            foreach (var k in dict.Items)
            {
                switch (k.Key)
                {
                case "id": ID = context.FromJson <long>(k.Value); break;

                case "url": Uri = new Uri(context.FromJson <string>(k.Value)); break;

                case "title": Title = context.FromJson <string>(k.Value); break;

                case "created_by": Author = context.FromJson <string>(k.Value); break;

                case "description": Description = context.FromJson <string>(k.Value); break;

                case "term_count": TermCount = context.FromJson <int>(k.Value); break;

                case "created_date": Created = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(k.Value)); break;

                case "modified_date": Modified = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(k.Value)); break;

                case "subjects": Subjects = context.FromJson <List <string> >(k.Value); break;

                case "visibility": Visibility = context.FromJson <string>(k.Value); break;

                case "editable": Editable = context.FromJson <string>(k.Value); break;

                case "has_access": HasAccess = context.FromJson <bool>(k.Value); break;

                case "has_discussion": HasDiscussion = context.FromJson <bool>(k.Value); break;

                case "lang_terms": TermLanguageCode = context.FromJson <string>(k.Value); break;

                case "lang_definitions": DefinitionLanguageCode = context.FromJson <string>(k.Value); break;

                case "terms":
                    var list = new List <TermInfo>();
                    if (k.Value is JsonArray)
                    {
                        foreach (var termJson in ((JsonArray)k.Value).Items)
                        {
                            if (!(termJson is JsonDictionary))
                            {
                                throw new JsonConvertException("Expected SetInfo.Terms to be an array of JSON dictionaries");
                            }
                            var term = new TermInfo {
                                ID         = context.FromJson <long>(((JsonDictionary)termJson).Items["id"]),
                                Term       = context.FromJson <string>(((JsonDictionary)termJson).Items["term"]),
                                Definition = context.FromJson <string>(((JsonDictionary)termJson).Items["definition"])
                            };
                            if (term.Term == null || term.Definition == null)
                            {
                                throw new JsonConvertException("Either term or definition was not group when converting from JSON to SetInfo");
                            }
                            list.Add(term);
                        }
                    }
                    else
                    {
                        throw new JsonConvertException("Expected SetInfo.Terms to be an array");
                    }
                    Terms = list;
                    break;
                }
            }

            // TODO: Validate that important fields are defined
        }
예제 #3
0
        public SetInfo(JsonValue json, IJsonContext context)
            : this()
        {
            var dict = json as JsonDictionary;
            if (dict == null)
                throw new JsonConvertException("Expected a JSON dictionary to be converted to a SetInfo");

            foreach (var k in dict.Items) {
                switch (k.Key) {
                    case "id": ID = context.FromJson<long>(k.Value); break;
                    case "url": Uri = new Uri(context.FromJson<string>(k.Value)); break;
                    case "title": Title = context.FromJson<string>(k.Value); break;
                    case "created_by": Author = context.FromJson<string>(k.Value); break;
                    case "description": Description = context.FromJson<string>(k.Value); break;
                    case "term_count": TermCount = context.FromJson<int>(k.Value); break;
                    case "created_date": Created = new DateTime(1970, 1, 1).AddSeconds(context.FromJson<long>(k.Value)); break;
                    case "modified_date": Modified = new DateTime(1970, 1, 1).AddSeconds(context.FromJson<long>(k.Value)); break;
                    case "subjects": Subjects = context.FromJson<List<string>>(k.Value); break;
                    case "visibility": Visibility = context.FromJson<string>(k.Value); break;
                    case "editable": Editable = context.FromJson<string>(k.Value); break;
                    case "has_access": HasAccess = context.FromJson<bool>(k.Value); break;
                    case "has_discussion": HasDiscussion = context.FromJson<bool>(k.Value); break;
                    case "lang_terms": TermLanguageCode = context.FromJson<string>(k.Value); break;
                    case "lang_definitions": DefinitionLanguageCode = context.FromJson<string>(k.Value); break;
                    case "terms":
                        var list = new List<TermInfo>();
                        if (k.Value is JsonArray) {
                            foreach (var termJson in ((JsonArray)k.Value).Items) {
                                if (!(termJson is JsonDictionary))
                                    throw new JsonConvertException("Expected SetInfo.Terms to be an array of JSON dictionaries");
                                var term = new TermInfo {
                                    ID = context.FromJson<long>(((JsonDictionary)termJson).Items["id"]),
                                    Term = context.FromJson<string>(((JsonDictionary)termJson).Items["term"]),
                                    Definition = context.FromJson<string>(((JsonDictionary)termJson).Items["definition"])
                                };
                                if (term.Term == null || term.Definition == null)
                                    throw new JsonConvertException("Either term or definition was not group when converting from JSON to SetInfo");
                                list.Add(term);
                            }
                        } else {
                            throw new JsonConvertException("Expected SetInfo.Terms to be an array");
                        }
                        Terms = list;
                        break;
                }
            }

            // TODO: Validate that important fields are defined
        }