Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON parser
#1
Hi Gintaras,
Would it be hard to incorporate a JSON parser similar to the IXml and IXmlNode interfaces already in QM?
There is something from MS called System.Runtime.Serialization.Json
https://msdn.microsoft.com/en-us/library...10%29.aspx

Is there a .NET limitation of QM for this?

Thanks,
Stuart
#2
Function JsonToXml
Code:
Copy      Help
;/
function'IXml $JSON [flags] ;;flags: 1 display XML text in QM output

;Converts JSON text to XML and returns IXml object.

;REMARKS
;On Windows XP SP2 and Vista must be installed .NET 3.5 or later. Older OS are not supported.

;EXAMPLE
;out
;str JSON=
;;{
;;;;;"hello": "world",
;;;;;"t": true ,
;;;;;"f": false,
;;;;;"n": null,
;;;;;"i": 123,
;;;;;"pi": 3.14,
;;;;;"Address": { "City": "New York", "State": "NY" },
;;;;;"a": [1, 2, 3, 4]
;;}
;IXml x=JsonToXml(JSON 1)
;IXmlNode r=x.RootElement
;;get simple
;out r.Child("hello").Value
;;get with XPath
;out r.Path("Address/State").Value
;;get array
;ARRAY(IXmlNode) a; r.Path("a/*" a)
;int i; for(i 0 a.len) out a[i].Value


opt noerrorshere 1
CsScript x.SetOptions("references=System.Xml;System.Runtime.Serialization;System.ServiceModel.Web")
x.AddCode("")
_s=x.Call("ToXml" JSON)
if(flags&1) out _s
IXml k._create
k.FromString(_s)
ret k


#ret
using System;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Xml;
using System.Xml.Linq;

public class Json
{
static public string ToXml(string JSON)
{
XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(JSON), new System.Xml.XmlDictionaryReaderQuotas());
return XElement.Load(reader).ToString();
}

}
#3
Wow....is there anything QM (and Gintaras) cant do?
Thanks so much! !

S
#4
cant seem to make this function work  even using the example i get the following

Error (RT) in <open ":1674: /232">Function19:  0x80131600, 
<macro "Scripting_Link /2 9 2 ''JsonToXml''">JsonToXml(2,9): error CS1010: Newline in constant
<macro "Scripting_Link /2 9 2 ''JsonToXml''">JsonToXml(2,9): error CS1012: Too many characters in character literal
<macro "Scripting_Link /2 1 2 ''JsonToXml''">JsonToXml(2,1): error CS0116: A namespace cannot directly contain members such as fields or methods
<macro "Scripting_Link /20 12 2 ''JsonToXml''">JsonToXml(20,12): error CS1001: Identifier expected
<macro "Scripting_Link /21 3 2 ''JsonToXml''">JsonToXml(21,3): error CS1022: Type or namespace definition, or end-of-file expected.    <help #IDP_ERR>?
#5
Maybe not all code was copied. This error would be if the code does not have the #ret line.
#6
yep thanks that was it somehow did not paste all the code. Works now.  I must be tired, can't believe i didn't catch that . Thanks again


Forum Jump:


Users browsing this thread: 3 Guest(s)