Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

Re: JSON Special Characters?

$
0
0
Re: JSON Special Characters?
Web Connection 5.0
Re: JSON Special Characters?
Nov. 13, 2012
01:40 am
3N603KXEGShow this entire thread in new window
Hi Todd,

Ok, so I spent a few hours today fixing this once and for all by creating a .NET wrapper around JSON.NET and exposing that to VFP for deserialization. There is now a new DeserializeJson() method in wwJsonSerializer that handles deserialization reliably with a system level component.

Attached are updated files that make this possible. Basically the new code uses an updated wwDotnetBridge and the JSON.NET library to do dynamic JSON parsing and passing the deserialization meta data back into FoxPro for consumption. This also makes the deserialization much less brittle - it should now work with just about any kind of JSON content no matter how its formatted.

To use this the code only has to change slightly:

do wwDotNetBridgeDO wwJsonSerializerLOCAL loSer as wwJsonSerializer loSer = CREATEOBJECT("wwJsonSerializer")CREATETABLE Test FREE (Name c(30),SomeText M, Entered D)TEXTTO lcText NOSHOW {\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\froman\fprq2 Arial;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\cf1\i\f0\fs20 Aid\i0 \par \cf0\fs18 \par }ENDTEXTSELECT testAPPENDBLANKreplace SomeText WITH lcText, NameWITH"Rick", ENtered WITHDATE() - 10APPENDBLANKreplace SomeText WITH lcText, NameWITH"Jimmy", ENtered WITHDATE() lcJson = loSer.Serialize("cursor:Test") ? lcText ? ? lcJson ? loCursor = loSer.DeserializeJson(lcJson) ? loCursor FOR lnX = 1 TO loCursor.Rows.Count loRow = loCursor.Rows.Item(lnX) ? loRow ? loRow.Name ? loRow.SomeText ? loRow.Entered ? "==="ENDFOR

Note that instead of Deserialize you need to use DeserializeJson() (this might change before I release this officially haven't decided yet)

This also requires .NET 2.0 or later installed to work. In preliminary tests performance is actually better than the manual parsing on larger items, which is an extra bonus.

Be interested for people to try this out with various values.

+++ Rick ---


That worked for me as well, but when you do it via a cursor it fails during the Deserialize with a Function argument, type, count or count is invalid. I'm doing this:

CLEARDO wwJsonSerializerCREATETABLE Test FREE (SomeText M)TEXTTO lcText NOSHOW {\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\froman\fprq2 Arial;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\cf1\i\f0\fs20 Aid\i0 \par \cf0\fs18 \par }ENDTEXTSELECT testAPPENDBLANKreplace SomeText WITH lcText loJson = CREATEOBJECT("wwJsonSerializer") lcJson = loJson.Serialize("cursor:Test") loDeserialized = loJson.Deserialize(lcJson) loRow = loDeserialized.Rows.Item(1) ?loRow.SomeText == Test.SomeText

The code peels off the Rows part correctly, but when it attempts to deal with the text is when the error occurs. It looks like the wwRegEx.Match function is failing to find a match, so you end up with a .f. being returned when it should be finding some text.

It's the "{" and the "}" in the lctext that is a problem. If your remove those, then it works:
<code="vfp">
TEXT TO lcText NOSHOW
{\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\froman\fprq2 Arial;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\i\f0\fs20 Aid\i0
\par \cf0\fs18
\par }
ENDTEXT
lcText = STRTRAN(lcText, "{", "QQQ")
lcText = STRTRAN(lcText, "}", "QQQ")


This should work.

I tried this:

CLEARDO wwJsonSerializerTEXTTO lcText NOSHOW {\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\froman\fprq2 Arial;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\cf1\i\f0\fs20 Aid\i0 blah blah blah \ul blah\ul (blah blah). \par \cf0\fs18 \par }ENDTEXT ? lcText ? ? loJson = CREATEOBJECT("wwJsonSerializer") lcJson = loJson.Serialize(lcText) ? lcJson ? ? lcDeSerialized = loJson.Deserialize(lcJson) ? lcDeserialized ? lcText == lcDeserialized

and it two-way serializes/deserializes properly with the result string equaling the original value.

+++ Rick ---


I have a memo field with rich text that I'm doing a JSON serialize with. When I deserialize that, the field is just a .t.. Other fields are working fine, other memo fields work fine, it's just the rich text data. Does JSON have some special characters that might would mess things up? The data looks something like this:

{\rtf1\ansi\deff0\deflang1033{\fonttbl{\f0\froman\fprq2 Arial;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\i\f0\fs20 Aid\i0 blah blah blah \ul blah\ul (blah blah).
\par \cf0\fs18
\par }





Rick Strahl
West Wind Technologies


from Maui, Hawaii

Making waves on the Web


Viewing all articles
Browse latest Browse all 10393

Trending Articles