Posts: 1,006
Threads: 330
Joined: Mar 2007
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
Posts: 12,073
Threads: 140
Joined: Dec 2002
Function
JsonToXml
;/
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();
}
}
Posts: 1,006
Threads: 330
Joined: Mar 2007
Wow....is there anything QM (and Gintaras) cant do?
Thanks so much! !
S
Posts: 1,336
Threads: 61
Joined: Jul 2006
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>?
Posts: 12,073
Threads: 140
Joined: Dec 2002
Maybe not all code was copied. This error would be if the code does not have the #ret line.
Posts: 1,336
Threads: 61
Joined: Jul 2006
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