protected override object ImportValue(ImportContext context, JsonReader reader)
        {
            string s = reader.ReadString().Trim();
        
            if (s.Length > 0)
            {
                char ch = s[0];
            
                if (Char.IsDigit(ch) || ch == '+' || ch == '-')
                    throw Error(s, null);
            }

            try
            {
                return Enum.Parse(OutputType, s, true);
            }
            catch (ArgumentException e)
            {
                //
                // Value is either an empty string ("") or only contains 
                // white space. Value is a name, but not one of the named
                // constants defined for the enumeration.
                //
            
                throw Error(s, e);
            }
        }
        public ITypeImporter Bind(ImportContext context, Type type)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (type == null)
                throw new ArgumentNullException("type");
            
            return (ITypeImporter) ImporterByType[type];
        }
        public ITypeImporter Bind(ImportContext context, Type type)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (type == null)
                throw new ArgumentNullException("type");

            return typeof(IJsonImportable).IsAssignableFrom(type) ? 
                new ImportAwareImporter(type) : null;
        }
        public ITypeImporter Bind(ImportContext context, Type type)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (type == null)
                throw new ArgumentNullException("type");

            return type.IsEnum && !type.IsDefined(typeof(FlagsAttribute), true) ? 
                   new EnumImporter(type) : null;
        }
        public ITypeImporter Bind(ImportContext context, Type type)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (type == null)
                throw new ArgumentNullException("type");
            
            return type.IsArray && type.GetArrayRank() == 1 ? 
                new ArrayImporter(type) : null;
        }
        public override object Import(ImportContext context, JsonReader reader)
        {
            if (reader == null) 
                throw new ArgumentNullException("reader");

            reader.MoveToContent();
            
            if (reader.TokenClass == JsonTokenClass.Null)
                return null;
            
            IJsonImportable o = CreateObject();
            o.Import(context, reader);
            return o;
        }
        public override object Import(ImportContext context, JsonReader reader)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (reader == null)
                throw new ArgumentNullException("reader");

            if (!reader.MoveToContent())
                throw new JsonException("Unexpected EOF.");
            
            if (reader.TokenClass == JsonTokenClass.Null)
            {
                reader.Read();
                return null;
            }

            Type elementType = OutputType.GetElementType();

            if (reader.TokenClass == JsonTokenClass.Array)
            {
                reader.Read();

                ArrayList list = new ArrayList();

                while (reader.TokenClass != JsonTokenClass.EndArray)
                    list.Add(context.Import(elementType, reader));

                reader.Read();
            
                return list.ToArray(elementType);
            }
            else if (reader.TokenClass == JsonTokenClass.String ||
                     reader.TokenClass == JsonTokenClass.Number ||
                     reader.TokenClass == JsonTokenClass.Boolean)
            {
                Array array = Array.CreateInstance(elementType, 1);
                array.SetValue(context.Import(elementType, reader), 0);
                return array;
            }
            else
            {
                throw new JsonException(string.Format("Found {0} where expecting JSON Array.", reader.TokenClass));
            }
        }
 public virtual object Import(ImportContext context, JsonReader reader)
 {
     if (context == null)
         throw new ArgumentNullException("context");
     
     if (reader == null)
         throw new ArgumentNullException("reader");
     
     if (!reader.MoveToContent())
         throw new JsonException("Unexpected EOF.");
     
     object o = null;
     
     if (reader.TokenClass != JsonTokenClass.Null)
         o = ImportValue(context, reader);
     
     reader.Read();
     return o;
 }
 public ITypeImporter Bind(ImportContext context, Type type)
 {
     return _importer != null  && _importer.OutputType == type ? _importer : null;
 }
 protected virtual object ImportValue(ImportContext context, JsonReader reader)
 {
     throw new NotImplementedException(); // FIXME: not implemented
 }
 private static string Json(string s)
 {
     ImportContext context = new ImportContext();
     return context.ImportAny(new JsonTextReader(new StringReader(s))).ToString();
 }
示例#12
0
 public virtual void Import(ImportContext context, JsonReader reader)
 {
     if (context == null)
         throw new ArgumentNullException("context");
     
     if (reader == null)
         throw new ArgumentNullException("reader");
     
     //
     // IMPORTANT! A new list is created and then committed to make
     // sure that this method is exception-safe. If something goes
     // wrong during the import of elements then this instance 
     // will remain largely untouched.
     //
     
     ArrayList list = new ArrayList();
     
     reader.ReadToken(JsonTokenClass.Array);
     
     while (reader.TokenClass != JsonTokenClass.EndArray)
         list.Add(context.ImportAny(reader));
     
     reader.Read();
     
     InnerList.Clear();
     InnerList.AddRange(list);
 }
 private object Parse(string source)
 {
     ImportContext context = new ImportContext();
     return context.ImportAny(new JsonTextReader(new StringReader(source)));
 }
 private static void AssertInStock(Type expected, Type type)
 {
     ImportContext context = new ImportContext();
     ITypeImporter importer = context.ImporterBinder.Bind(context, type);
     Assert.IsInstanceOfType(expected, importer, type.FullName);
 }
        public void YahooNewsSearch()
        {
            string text = @"
            /* Source: http://api.search.yahoo.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=yahoo&results=3&language=en&output=json */
            {
                ""ResultSet"": {
                    ""totalResultsAvailable"": ""2393"",
                    ""totalResultsReturned"": 3,
                    ""firstResultPosition"": ""1"",
                    ""Result"": [
                        {
                            ""Title"": ""Yahoo invites its users to shoot ads"",
                            ""Summary"": "" Yahoo first encouraged consumers to create blogs and photo pages with text and pictures. Now, the Internet portal wants them to make advertisements, too. On Monday, Yahoo touts a new look for its front page by asking people to pull out the video camera, open up the editing software and create 12-second spot for Yahoo."",
                            ""Url"": ""http://news.yahoo.com/s/usatoday/20060717/tc_usatoday/yahooinvitesitsuserstoshootads"",
                            ""ClickUrl"": ""http://news.yahoo.com/s/usatoday/20060717/tc_usatoday/yahooinvitesitsuserstoshootads"",
                            ""NewsSource"": ""USATODAY.com via Yahoo! News"",
                            ""NewsSourceUrl"": ""http://news.yahoo.com/"",
                            ""Language"": ""en"",
                            ""PublishDate"": ""1153133816"",
                            ""ModificationDate"": ""1153134044""
                        },
                        {
                            ""Title"": ""Yahoo to launch new finance features"",
                            ""Summary"": "" Yahoo Inc. is beefing up the finance section of its Web site with more interactive stock charts and other features to help it maintain its longtime lead over rival financial information sites."",
                            ""Url"": ""http://news.yahoo.com/s/ap/20060717/ap_on_hi_te/yahoo_finance_2"",
                            ""ClickUrl"": ""http://news.yahoo.com/s/ap/20060717/ap_on_hi_te/yahoo_finance_2"",
                            ""NewsSource"": ""AP via Yahoo! News"",
                            ""NewsSourceUrl"": ""http://news.yahoo.com/"",
                            ""Language"": ""en"",
                            ""PublishDate"": ""1153134777"",
                            ""ModificationDate"": ""1153134920"",
                            ""Thumbnail"": {
                                ""Url"": ""http://us.news2.yimg.com/us.yimg.com/p/ap/20060714/vsthumb.8b1161b66b564adba0a5bbd6339c9379.media_summit_idet125.jpg"",
                                ""Height"": ""82"",
                                ""Width"": ""76""
                            }
                        }, 
                        {
                            ""Title"": ""Yahoo Finance revises charts, chat, other features"",
                            ""Summary"": "" Yahoo Inc. on Monday will unveil an upgraded version of its top-ranked financial information site that features new stock charting tools, improved investor chat rooms and financial video news."",
                            ""Url"": ""http://news.yahoo.com/s/nm/20060717/wr_nm/media_yahoo_finance_dc_2"",
                            ""ClickUrl"": ""http://news.yahoo.com/s/nm/20060717/wr_nm/media_yahoo_finance_dc_2"",
                            ""NewsSource"": ""Reuters via Yahoo! News"",
                            ""NewsSourceUrl"": ""http://news.yahoo.com/"",
                            ""Language"": ""en"",
                            ""PublishDate"": ""1153113288"",
                            ""ModificationDate"": ""1153113674""
                        }
                    ]
                }
            }";
            
            JsonTextReader reader = new JsonTextReader(new StringReader(text));
            ImportContext context = new ImportContext();
            YahooResponse response = (YahooResponse) context.Import(typeof(YahooResponse), reader);
            Assert.IsNotNull(response);
            
            YahooResultSet resultSet = response.ResultSet;
            Assert.IsNotNull(resultSet);
            Assert.AreEqual(2393,  resultSet.totalResultsAvailable);
            Assert.AreEqual(3,  resultSet.totalResultsReturned);
            Assert.AreEqual(1,  resultSet.firstResultPosition);
            Assert.AreEqual(3,  resultSet.Result.Length);
            
            YahooResult result = resultSet.Result[0];
            
            Assert.IsNotNull(result);
            Assert.AreEqual("Yahoo invites its users to shoot ads", result.Title);
            Assert.AreEqual(" Yahoo first encouraged consumers to create blogs and photo pages with text and pictures. Now, the Internet portal wants them to make advertisements, too. On Monday, Yahoo touts a new look for its front page by asking people to pull out the video camera, open up the editing software and create 12-second spot for Yahoo.", result.Summary);
            Assert.AreEqual("http://news.yahoo.com/s/usatoday/20060717/tc_usatoday/yahooinvitesitsuserstoshootads", result.Url);
            Assert.AreEqual("http://news.yahoo.com/s/usatoday/20060717/tc_usatoday/yahooinvitesitsuserstoshootads", result.ClickUrl);
            Assert.AreEqual("USATODAY.com via Yahoo! News", result.NewsSource);
            Assert.AreEqual("http://news.yahoo.com/", result.NewsSourceUrl);
            Assert.AreEqual("en", result.Language);
            Assert.AreEqual(1153133816, result.PublishDate);
            Assert.AreEqual(1153134044, result.ModificationDate);
            
            result = resultSet.Result[1];

            Assert.AreEqual("Yahoo to launch new finance features", result.Title);
            Assert.AreEqual(" Yahoo Inc. is beefing up the finance section of its Web site with more interactive stock charts and other features to help it maintain its longtime lead over rival financial information sites.", result.Summary);
            Assert.AreEqual("http://news.yahoo.com/s/ap/20060717/ap_on_hi_te/yahoo_finance_2", result.Url);
            Assert.AreEqual("http://news.yahoo.com/s/ap/20060717/ap_on_hi_te/yahoo_finance_2", result.ClickUrl);
            Assert.AreEqual("AP via Yahoo! News", result.NewsSource);
            Assert.AreEqual("http://news.yahoo.com/", result.NewsSourceUrl);
            Assert.AreEqual("en", result.Language);
            Assert.AreEqual(1153134777, result.PublishDate);
            Assert.AreEqual(1153134920, result.ModificationDate);
            Assert.AreEqual("http://us.news2.yimg.com/us.yimg.com/p/ap/20060714/vsthumb.8b1161b66b564adba0a5bbd6339c9379.media_summit_idet125.jpg", result.Thumbnail.Url);
            Assert.AreEqual(82, result.Thumbnail.Height);
            Assert.AreEqual(76, result.Thumbnail.Width);

            result = resultSet.Result[2];

            Assert.AreEqual("Yahoo Finance revises charts, chat, other features", result.Title);
            Assert.AreEqual(" Yahoo Inc. on Monday will unveil an upgraded version of its top-ranked financial information site that features new stock charting tools, improved investor chat rooms and financial video news.", result.Summary);
            Assert.AreEqual("http://news.yahoo.com/s/nm/20060717/wr_nm/media_yahoo_finance_dc_2", result.Url);
            Assert.AreEqual("http://news.yahoo.com/s/nm/20060717/wr_nm/media_yahoo_finance_dc_2", result.ClickUrl);
            Assert.AreEqual("Reuters via Yahoo! News", result.NewsSource);
            Assert.AreEqual("http://news.yahoo.com/", result.NewsSourceUrl);
            Assert.AreEqual("en", result.Language);
            Assert.AreEqual(1153113288, result.PublishDate);
            Assert.AreEqual(1153113674, result.ModificationDate);
        }
 private static object Import(Type expectedType, string s)
 {
     JsonReader reader = CreateReader(s);
     ImportContext context = new ImportContext();
     object o = context.Import(expectedType, reader);
     Assert.IsNotNull(o);
     Assert.IsInstanceOfType(expectedType, o);
     return o;
 }
 public void Registration()
 {
     Type type = typeof(Thing);
     TypeImporterCollection binders = new TypeImporterCollection();
     ImportContext context = new ImportContext();
     Assert.IsNull(binders.Bind(context, type));
     ImportAwareImporter importer = new ImportAwareImporter(type);
     binders.Add(importer);
     Assert.AreSame(importer, binders.Bind(context, type));
 }
 public void Import(ImportContext context, JsonReader reader)
 {
     ImportCalled = true;
 }
 public void Import(ImportContext context, JsonReader reader)
 {
     throw new NotImplementedException();
 }
示例#20
0
        /// <remarks>
        /// This method is not exception-safe. If an error occurs while 
        /// reading then the object may be partially imported.
        /// </remarks>

        public virtual void Import(ImportContext context, JsonReader reader)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (reader == null)
                throw new ArgumentNullException("reader");

            // FIXME: Consider making this method exception-safe.
            // Right now this is a problem because of reliance on
            // DictionaryBase.
            
            Clear();
            
            reader.ReadToken(JsonTokenClass.Object);
            
            while (reader.TokenClass != JsonTokenClass.EndObject)
                Put(reader.ReadMember(), context.ImportAny(reader));
            
            reader.Read();
        }