Friday, December 19, 2008
Visual Foxpro JSON Class Update
JSON Class Update
Since my
initial post
regarding this class, work has continued improving it. A number of fixes and enhancement can be found in the latest version...
Fixes to parse method
- There was a problem with brace matching for "{}" and "[]" when nested in certain ways.
Fixes to UseUTCDatetime functionality
- rules are now as follows:
UseUTCDateTime = .F. && Datetimes will stringify including Local offset from UTC
UseUTCDateTime = .T. && Datetimes will stringify as UTC equivalent for Local
Parse() && will always return Local equivalent
Fixes to GetTimezoneOffset method
- now respects Standard/Daylight boundaries of datetimes
Fixed Stringify
- cursors are serialized to valid JSON now
Added KeyForCursor property
- this allows a JSON key to be specified for identifying cursors (defaulted to "VFPData")
Enhanced Stringify and Parse methods
- they now handle the VFP Collection class with aplomb
Added KeyForItems property
- when serializing Collections, the key for the contained JSON object's array property can be defined (defaulted to "items")
Renamed DateSerializationType property
- it is now ParseDateType
Enhanced Parse
- JSON class will now (optionally) respect Class and ClassLibrary properties if they exist in a JSON object being deserialized (see ParseRespectClass property). This means that if you Stringify() a VFP Form and then Parse() the JSON (with ParseRespectClass = .T.) you will get a Form object back rather than an Empty object with the same properties as the Form.
I would consider the JSON class production-ready at this point. This doesn't mean that bugs won't be found in the future, but it has been hammered on enough that I wouldn't hesitate to use it in a production application.
Visual FoxPro Now Listed
I got in touch with
Douglas Crockford
and was able to get Visual FoxPro listed on
JSON.org
with a link to my initial post. I appreciate Douglas Crockford taking the time out of his busy schedule to list Visual FoxPro on the JSON site.
Special Thank You
Nearly all of the fixes and enhancements provided in this version of the JSON class were made possible by the help and prodding I received from Frank Dietrich of Berlin. He is an extremely bright fellow. Implementing the Timezone and Daylight vs. Standard stuff drove us both to the brink. I sincerely doubt I could have done it without his help.
Download the JSON class library (approx. 59 KB)
The download contains the JSON class in both VCX and PRG format. They are the same. Use whichever one you prefe
r.
Information Technology
|
Visual Foxpro
Friday, December 19, 2008 9:43:59 AM (GMT Standard Time, UTC+00:00)
Comments [3]
Sunday, January 18, 2009 7:05:11 AM (GMT Standard Time, UTC+00:00)
Great class, Craig! I've been using it in production with no issues at all.
It's clearly documented that if one wants to serialize an array, one needs to pass it by reference.
HOWEVER now I have a situation where I have an array of rows as a property of an object.
How can I serialize an array member as part of serializing the object itself? I tried passing the "@array" property as part of the tvReplacer property but that doesn't seem to work.
Any ideas? Thanks!
Test code:
SET PROCEDURE TO Json ADDITIVE
o= CREATEOBJECT("Empty")
ADDPROPERTY(o, "Myarray[3]")
o.MyArray[1]="Serialize"
o.MyArray[2]="all"
o.MyArray[3]="of me!"
oJSON= CREATEOBJECT("Json")
CLEAR
? oJSON.Stringify(o) && {"myarray": "Serialize"}
* ?oJSON.Stringify(o,"@MyArray") && Crash
DIMENSION aReplacer[1]
aReplacer[1]="@myarray"
? oJSON.Stringify(o,@aReplacer) && {}
Steven Black
Sunday, March 08, 2009 7:31:01 PM (GMT Standard Time, UTC+00:00)
Thanks for reporting this Steven. Sorry it has taken me so long to get to this.
The problem is in the PROTECTED PROCEDURE serializeobject around line 919 the code should read something like the following...
m.lcPropertyFullname = "m.toObject." + m.lcKey
IF TYPE(m.lcPropertyFullname) != "U" && members like ACTIVECONTROL may not be an object
IF TYPE(m.lcPropertyFullname, 1) != "A"
m.lvMemberValue = GETPEM(m.toObject, m.lcKey)
ELSE
RELEASE lvMemberValue
=ACOPY(&lcPropertyFullname, lvMemberValue)
ENDIF
ELSE
m.lvMemberValue = NULL
ENDIF
Craig Boyd
Sunday, March 08, 2009 7:41:17 PM (GMT Standard Time, UTC+00:00)
I've uploaded an updated version of the JSON library. Redownload it and the problem should be solved.
Craig Boyd
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your
gravatar
icon)
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
© Copyright 2012 Craig Boyd
|
|
| Page rendered at Thursday, February 09, 2012 1:32:29 AM (GMT Standard Time, UTC+00:00)
Archive
<
February 2012
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
On this page....
Navigation
Blog Posts by Month/Year
SPS Blog Home
Visit SweetPotato Software
Contact Craig Boyd
Categories
General
Information Technology
Personal
Visual Foxpro
Blogroll
Andrew MacNeill
Andy Kramek
Bernard Bout
Bo Durban
Calvin Hsia
Cathy Pountney
Cesar Chalom
Christof Wollenhaupt
Craig Bailey
Doug Hennig
Foxpro.catalyst
Kok Kiet
Lisa Slater Nicholls
Rick Borup
Rick Schummer
Rick Strahl
Sergey Berezniker
Sign In