コード例 #1
0
ファイル: JsonValueFormatter.cs プロジェクト: nuxleus/WCFWeb
 private static JsonValue DeserializeFromJXML(Message message)
 {
     using (XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents())
     {
         return(JsonValueExtensions.Load(bodyReader));
     }
 }
コード例 #2
0
        public void LoadFromXmlTest()
        {
            string json = "{\"a\":123,\"b\":[false,null,12.34]}";
            string xml  = "<root type=\"object\"><a type=\"number\">123</a><b type=\"array\"><item type=\"boolean\">false</item><item type=\"null\"/><item type=\"number\">12.34</item></b></root>";

            using (XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(Encoding.UTF8.GetBytes(xml), XmlDictionaryReaderQuotas.Max))
            {
                JsonValue jv = JsonValueExtensions.Load(xdr);
                Assert.AreEqual(json, jv.ToString());
            }
        }
コード例 #3
0
        public void LoadFromXmlJsonReaderTest()
        {
            string json = "{\"a\":123,\"b\":[false,null,12.34]}";

            byte[]    jsonBytes = Encoding.UTF8.GetBytes(json);
            JsonValue jv;

            using (XmlDictionaryReader xdr = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max))
            {
                jv = JsonValueExtensions.Load(xdr);
            }

            Assert.AreEqual(json, jv.ToString());

            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => JsonValueExtensions.Load((XmlDictionaryReader)null));
        }
コード例 #4
0
        public void TestBadJXMLMapping()
        {
            List <string> badJXMLs = new List <string>
            {
                "<item type='array'/>",
                "<?xml version='1.0'?>",
                "<root type='array'><notItem type='string'>hello</notItem></root>",
                "<root type='array'><item type='unknown'>hello</item></root>",
                "<root type='object'><item:item xmlns:item='item' type='string'>foo</item:item></root>",
            };

            foreach (string badJXML in badJXMLs)
            {
                Log.Info("Bad JXML: {0}", badJXML);
                byte[] xmlBytes = Encoding.UTF8.GetBytes(badJXML);
                ExpectException <FormatException>(() => JsonValueExtensions.Load(XmlDictionaryReader.CreateTextReader(xmlBytes, XmlDictionaryReaderQuotas.Max)));
            }
        }
コード例 #5
0
        public void TestJXMLMapping()
        {
            string completeJson = "{\"a\":[123,\"hello\",true,null,{}]}";
            string completeJxml = @"<root type='object'>
  <a type='array'>
    <item type='number'>123</item>
    <item type='string'>hello</item>
    <item type='boolean'>true</item>
    <item type='null'></item>
    <item type='object'/>
  </a>
</root>";
            List <Tuple <string, string> > jsonAndJxmlPairs = new List <Tuple <string, string> >
            {
                new Tuple <string, string>(completeJson, completeJxml),
                new Tuple <string, string>("[]", "<root type='array'/>"),
                new Tuple <string, string>("[]", "<root type='array'></root>"),
                new Tuple <string, string>("[]", "<?xml version='1.0'?>   <root type='array'/>"),
                new Tuple <string, string>("{}", "<root type='object'/>"),
                new Tuple <string, string>("{}", "<root type='object'></root>"),
                new Tuple <string, string>("\"hello\"", "<root type='string'>hello</root>"),
                new Tuple <string, string>("\"hello\"", "<root>hello</root>"),
                new Tuple <string, string>("\"\"", "<root></root>"),
                new Tuple <string, string>("\"\"", "<root type='string'/>"),
                new Tuple <string, string>("\"\"", "<root/>"),
                new Tuple <string, string>("[1,\"1\"]", "<root type='array'><item type='number'>1</item><item type='string'>1</item></root>"),
                new Tuple <string, string>("[null,null]", "<root type='array'><item type='null'></item><item type='null'/></root>"),
            };

            foreach (var pair in jsonAndJxmlPairs)
            {
                string json = pair.Item1;
                string jxml = pair.Item2;
                Log.Info("Testing with JSON '{0}' and JXML '{1}'", json, jxml);
                byte[] jxmlBytes = Encoding.UTF8.GetBytes(jxml);
                using (XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(jxmlBytes, XmlDictionaryReaderQuotas.Max))
                {
                    JsonValue jv = JsonValueExtensions.Load(xdr);
                    Assert.AreEqual(json, jv.ToString());
                }
            }
        }