Professional Documents
Culture Documents
h"
*-- If .T. then all datetimes will be serialized to UTC equivalents and
deserialized as UTC. If .F. then all datetimes will be conversely treated as Local
datetimes.
useutcdatetime = .F.
PROTECTED name
PROTECTED classlibrary
PROTECTED addobject
PROTECTED addproperty
PROTECTED baseclass
PROTECTED class
PROTECTED cloneobject
PROTECTED comment
PROTECTED controlcount
PROTECTED controls
PROTECTED destroy
PROTECTED error
PROTECTED height
PROTECTED helpcontextid
PROTECTED newobject
PROTECTED objects
PROTECTED parent
PROTECTED parentclass
PROTECTED picture
PROTECTED readexpression
PROTECTED readmethod
PROTECTED removeobject
PROTECTED resettodefault
PROTECTED saveasclass
PROTECTED showwhatsthis
PROTECTED tag
PROTECTED whatsthishelpid
PROTECTED width
PROTECTED writeexpression
PROTECTED writemethod
PROCEDURE stringify
LPARAMETERS tvValue, tvReplacer, tvSpace, tnLevel, tcKey
*********************************************************************
*!* PURPOSE
*********************************************************************
*!* Serialize VFP objects, values, arrays and cursors to JSON
*********************************************************************
*!* PARAMETERS
*********************************************************************
*!* tvValue: (required)
*!* Either an instance of a VFP object/value/array or an
Alias that will be stringified.
*!* Arrays must be sent in byref...
JSON.Stringify(@MyArray)... otherwise only the first
*!* element of the array will be stringified
*!* tvReplacer: (optional)
*!* The optional tvReplacer parameter is either a string
specifying a Function/Object.Method,
*!* a byref Array or a string containing a comma-
delimited list of keys.
*!* If cFunction/cMethod is sent in it will be used via
Evaluate. It is sent the parent object
*!* and each of the member keys and value pairs.
Evaluate(m.tcReviver(parent, membername, value)'s
*!* return value is then serialized instead of the
original object.
*!* If the return value equals This.Undefined, then the
object is not serialized.
*!* If Array/CSV is sent in then it contains a list of
keys (member names) that should be serialized.
*!* All other keys (members) will be ignored.
*!* tvSpace: (optional)
*!* A string to be used with line-breaks to indent the
JSON produced, or it can be numeric specifying
*!* the number of spaces to use for indention. It simply
makes the JSON easier to read by beautifying it.
*!* tnLevel: (internal-use)
*!* Tracks the hierarchy levels used for proper indention
of JSON when tvSpace is sent in.
*!* tcKey: (internal-use)
*!* Allows the keys (member names) to be sent in when
stringifying key/value pairs.
*********************************************************************
*!* RETURN
*********************************************************************
*!* String - JSON representation of tvValue sent in
*********************************************************************
IF VARTYPE(m.tcKey) = "C"
m.lcKey = ["] + m.tcKey + [": ]
ELSE
m.lcKey = ""
ENDIF
m.lcTypeOfValue = TYPE("m.tvValue", 1)
IF m.lcTypeOfValue != "A" && if it's not an array then let's get the
Type
m.lcTypeOfValue = VARTYPE(m.tvValue, .F.)
IF m.lcTypeOfValue = "O"
*!* ToJSON() method shouldn't serialize, simply pass back a
value or undefined
IF PEMSTATUS(m.tvValue, "tojson", 5) && if object has a
ToJSON() method then use it to get value to serialize
m.lvValue = EVALUATE("m.tvValue.tojson()")
ELSE
m.lvValue = m.tvValue
ENDIF
ELSE
m.lvValue = m.tvValue
ENDIF
ENDIF
IF !THIS.IsUndefined(m.lcReturnJSON)
m.lcIndent = THIS.GetIndentChars(m.tvSpace, m.tnLevel)
IF m.tnLevel != 0
m.lcReturnJSON = m.lcIndent + m.lcKey + m.lcReturnJSON
ENDIF
ENDIF
RETURN (m.lcReturnJSON)
*********************************************************************
*!* ADDITIONAL NOTES AND COMMENTS
*********************************************************************
*!* Online JSON Validator
*!* http://www.jsonlint.com/
*********************************************************************
ENDPROC