forked from sait/vfpjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.prg
734 lines (640 loc) · 15.9 KB
/
json.prg
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
*
* vfpjson
*
* ----------------------------------
* Ignacio Gutiérrez Torrero
* SAIT Software Administrativo
* www.sait.com.mx
* +52(653)534-8800
* Monterrey México
* -----------------------------------
*
* JSON Library in VFP
* Libreria para el manejo de JSON en VFP
*
* http://code.google.com/p/dart/source/browse/trunk/dart/lib/json/json.dart
* Thanks Google for the code in Json Dart
* Gracias a Google por el codigo de Json de Dart
*
* json_encode(xExpr)
* returns a string, that is the json of any expression passed
*
* json_decode(cJson)
* returns an object, from the string passed
*
* json_getErrorMsg()
* returns empty if no error found in last decode.
*
*
*
* Examples:
*
* set procedure json additive
* oPerson = json_decode(' { "name":"Ignacio" , "lastname":"Gutierrez", "age":33 } ')
* if not empty(json_getErrorMsg())
* ? 'Error in decode:'+json_getErrorMsg())
* return
* endif
* ? oPerson.get('name') , oPerson.get('lastname')
*
*
* oJson = newobject('json','json.prg')
* oCustomer = oJson.decode( ' { "name":"Ignacio" , "lastname":"Gutierrez", "age":33 } ')
* ? oJson.encode(oCustomer)
* ? oCustomer.get('name')
* ? oCustomer.get('lastname')
*
* obj = oJson.decode('{"jsonrpc":"1.0", "id":1, "method":"sumArray", "params":[3.1415,2.14,10],"version":1.0}')
* ? obj.get('jsonrpc'), obj._jsonrpc
* ? obj.get('id'), obj._id
* ? obj.get('method'), obj._method
* ? obj._Params.array[1], obj._Params.get(1)
*
*
*
*
lRunTest = .t.
if lRunTest
testJsonClass()
endif
return
function json_encode(xExpr)
if vartype(_json)<>'O'
public _json
_json = newobject('json')
endif
return _json.encode(@xExpr)
function json_decode(cJson)
local retval
if vartype(_json)<>'O'
public _json
_json = newobject('json')
endif
retval = _json.decode(cJson)
if not empty(_json.cError)
return null
endif
return retval
function json_getErrorMsg()
return _json.cError
*
* recordToJson()
*
* Returns the json representation for current record
* Try it:
* use c:\mydir\mytable
* cInfo = recordToJson()
* ? cInfo
*
function recordToJson
local nRecno,i,oObj, cRetVal
if alias()==''
return ''
endif
oObj = newObject('myObj')
for i=1 to fcount()
oObj.set(Field(i),eval(Field(i)))
next
cRetVal = json_encode(oObj)
if not empty(json_getErrorMsg())
cRetVal = 'ERROR:'+json_getErrorMsg()
endif
return cRetVal
*
* tableToJson()
*
* Returns the json representation for current table
* Warning need to be changed for large table, because use dimension aInfo[reccount()]
* For large table should change to create the string record by record.
*
* Try it:
* use c:\mydir\mytable
* cInfo = tableToJson()
* ? cInfo
* _cliptext = strtran(cInfo, ',{"', ','+chr(13)+'{"')
* Go to Any Editor and Paste the information
*
function tableToJson
local nRecno,i,oObj, cRetVal,nRec
if alias()==''
return ''
endif
nRecno = recno()
nRec = 1
dimension aInfo[1]
scan
oObj = newObject('myObj')
for i=1 to fcount()
oObj.set(Field(i),eval(Field(i)))
next
dimension aInfo[nRec]
aInfo[nRec] = oObj
nRec = nRec+1
endscan
goto nRecno
cRetVal = json_encode(@aInfo)
if not empty(json_getErrorMsg())
cRetVal = 'ERROR:'+json_getErrorMsg()
endif
return cRetVal
*
* json class
*
*
define class json as custom
nPos=0
nLen=0
cJson=''
cError=''
*
* Genera el codigo cJson para parametro que se manda
*
function encode(xExpr)
local cTipo
* Cuando se manda una arreglo,
if type('ALen(xExpr)')=='N'
cTipo = 'A'
Else
cTipo = VarType(xExpr)
Endif
Do Case
Case cTipo=='D'
return '"'+dtos(xExpr)+'"'
Case cTipo=='N'
return Transform(xExpr)
Case cTipo=='L'
return iif(xExpr,'true','false')
Case cTipo=='X'
return 'null'
Case cTipo=='C'
xExpr = allt(xExpr)
xExpr = StrTran(xExpr, '\', '\\' )
xExpr = StrTran(xExpr, '/', '\/' )
xExpr = StrTran(xExpr, Chr(9), '\t' )
xExpr = StrTran(xExpr, Chr(10), '\n' )
xExpr = StrTran(xExpr, Chr(13), '\r' )
xExpr = StrTran(xExpr, '"', '\"' )
return '"'+xExpr+'"'
case cTipo=='O'
local cProp, cJsonValue, cRetVal, aProp[1]
=AMembers(aProp,xExpr)
cRetVal = ''
for each cProp in aProp
*?? cProp,','
*? cRetVal
if type('xExpr.'+cProp)=='U' or cProp=='CLASS'
* algunas propiedades pueden no estar definidas
* como: activecontrol, parent, etc
loop
endif
if type( 'ALen(xExpr.'+cProp+')' ) == 'N'
*
* es un arreglo, recorrerlo usando los [ ] y macro
*
Local i,nTotElem
cJsonValue = ''
nTotElem = Eval('ALen(xExpr.'+cProp+')')
For i=1 to nTotElem
cmd = 'cJsonValue=cJsonValue+","+ this.encode( xExpr.'+cProp+'[i])'
&cmd.
Next
cJsonValue = '[' + substr(cJsonValue,2) + ']'
else
*
* es otro tipo de dato normal C, N, L
*
cJsonValue = this.encode( evaluate( 'xExpr.'+cProp ) )
endif
if left(cProp,1)=='_'
cProp = substr(cProp,2)
endif
cRetVal = cRetVal + ',' + '"' + lower(cProp) + '":' + cJsonValue
next
return '{' + substr(cRetVal,2) + '}'
case cTipo=='A'
local valor, cRetVal
cRetVal = ''
for each valor in xExpr
cRetVal = cRetVal + ',' + this.encode( valor )
next
return '[' + substr(cRetVal,2) + ']'
endcase
return ''
*
* regresa un elemento representado por la cadena json que se manda
*
function decode(cJson)
local retValue
cJson = StrTran(cJson,chr(9),'')
cJson = StrTran(cJson,chr(10),'')
cJson = StrTran(cJson,chr(13),'')
cJson = this.fixUnicode(cJson)
this.nPos = 1
this.cJson = cJson
this.nLen = len(cJson)
this.cError = ''
retValue = this.parsevalue()
if not empty(this.cError)
return null
endif
if this.getToken()<>null
this.setError('Junk at the end of JSON input')
return null
endif
return retValue
function parseValue()
local token
token = this.getToken()
if token==null
this.setError('Nothing to parse')
return null
endif
do case
case token=='"'
return this.parseString()
case isdigit(token) or token=='-'
return this.parseNumber()
case token=='n'
return this.expectedKeyword('null',null)
case token=='f'
return this.expectedKeyword('false',.f.)
case token=='t'
return this.expectedKeyword('true',.t.)
case token=='{'
return this.parseObject()
case token=='['
return this.parseArray()
otherwise
this.setError('Unexpected token')
endcase
return
function expectedKeyword(cWord,eValue)
for i=1 to len(cWord)
cChar = this.getChar()
if cChar <> substr(cWord,i,1)
this.setError("Expected keyword '" + cWord + "'")
return ''
endif
this.nPos = this.nPos + 1
next
return eValue
function parseObject()
local retval, cPropName, xValue
retval = createObject('myObj')
this.nPos = this.nPos + 1 && Eat {
if this.getToken()<>'}'
do while .t.
cPropName = this.parseString()
if not empty(this.cError)
return null
endif
if this.getToken()<>':'
this.setError("Expected ':' when parsing object")
return null
endif
this.nPos = this.nPos + 1
xValue = this.parseValue()
if not empty(this.cError)
return null
endif
** Debug ? cPropName, type('xValue')
retval.set(cPropName, xValue)
if this.getToken()<>','
exit
endif
this.nPos = this.nPos + 1
enddo
endif
if this.getToken()<>'}'
this.setError("Expected '}' at the end of object")
return null
endif
this.nPos = this.nPos + 1
return retval
function parseArray()
local retVal, xValue
retval = createObject('MyArray')
this.nPos = this.nPos + 1 && Eat [
if this.getToken() <> ']'
do while .t.
xValue = this.parseValue()
if not empty(this.cError)
return null
endif
retval.add( xValue )
if this.getToken()<>','
exit
endif
this.nPos = this.nPos + 1
enddo
if this.getToken() <> ']'
this.setError('Expected ] at the end of array')
return null
endif
endif
this.nPos = this.nPos + 1
return retval
function parseString()
local cRetVal, c
if this.getToken()<>'"'
this.setError('Expected "')
return ''
endif
this.nPos = this.nPos + 1 && Eat "
cRetVal = ''
do while .t.
c = this.getChar()
if c==''
return ''
endif
if c == '"'
this.nPos = this.nPos + 1
exit
endif
if c == '\'
this.nPos = this.nPos + 1
if (this.nPos>this.nLen)
this.setError('\\ at the end of input')
return ''
endif
c = this.getChar()
if c==''
return ''
endif
do case
case c=='"'
c='"'
case c=='\'
c='\'
case c=='/'
c='/'
case c=='b'
c=chr(8)
case c=='t'
c=chr(9)
case c=='n'
c=chr(10)
case c=='f'
c=chr(12)
case c=='r'
c=chr(13)
otherwise
******* FALTAN LOS UNICODE
this.setError('Invalid escape sequence in string literal')
return ''
endcase
endif
cRetVal = cRetVal + c
this.nPos = this.nPos + 1
enddo
return cRetVal
**** Pendiente numeros con E
function parseNumber()
local nStartPos,c, isInt, cNumero
if not ( isdigit(this.getToken()) or this.getToken()=='-')
this.setError('Expected number literal')
return 0
endif
nStartPos = this.nPos
c = this.getChar()
if c == '-'
c = this.nextChar()
endif
if c == '0'
c = this.nextChar()
else
if isdigit(c)
c = this.nextChar()
do while isdigit(c)
c = this.nextChar()
enddo
else
this.setError('Expected digit when parsing number')
return 0
endif
endif
isInt = .t.
if c=='.'
c = this.nextChar()
if isdigit(c)
c = this.nextChar()
isInt = .f.
do while isDigit(c)
c = this.nextChar()
enddo
else
this.setError('Expected digit following dot comma')
return 0
endif
endif
cNumero = substr(this.cJson, nStartPos, this.nPos - nStartPos)
return val(cNumero)
function getToken()
local char1
do while .t.
if this.nPos > this.nLen
return null
endif
char1 = substr(this.cJson, this.nPos, 1)
if char1==' '
this.nPos = this.nPos + 1
loop
endif
return char1
enddo
return
function getChar()
if this.nPos > this.nLen
this.setError('Unexpected end of JSON stream')
return ''
endif
return substr(this.cJson, this.nPos, 1)
function nextChar()
this.nPos = this.nPos + 1
if this.nPos > this.nLen
return ''
endif
return substr(this.cJson, this.nPos, 1)
function setError(cMsg)
this.cError= 'ERROR parsing JSON at Position:'+allt(str(this.nPos,6,0))+' '+cMsg
return
function getError()
return this.cError
function fixUnicode(cStr)
cStr = StrTran(cStr,'\u00e1','á')
cStr = StrTran(cStr,'\u00e9','é')
cStr = StrTran(cStr,'\u00ed','í')
cStr = StrTran(cStr,'\u00f3','ó')
cStr = StrTran(cStr,'\u00fa','ú')
cStr = StrTran(cStr,'\u00c1','Á')
cStr = StrTran(cStr,'\u00c9','É')
cStr = StrTran(cStr,'\u00cd','Í')
cStr = StrTran(cStr,'\u00d3','Ó')
cStr = StrTran(cStr,'\u00da','Ú')
cStr = StrTran(cStr,'\u00f1','ñ')
cStr = StrTran(cStr,'\u00d1','Ñ')
return cStr
enddefine
*
* class used to return an array
*
define class myArray as custom
nSize = 0
dimension array[1]
function add(xExpr)
this.nSize = this.nSize + 1
dimension this.array[this.nSize]
this.array[this.nSize] = xExpr
return
function get(n)
return this.array[n]
function getsize()
return this.nSize
enddefine
*
* class used to simulate an object
* all properties are prefixed with 'prop' to permit property names like: error, init
* that already exists like vfp methods
*
define class myObj as custom
Hidden ;
ClassLibrary,Comment, ;
BaseClass,ControlCount, ;
Controls,Objects,Object,;
Height,HelpContextID,Left,Name, ;
Parent,ParentClass,Picture, ;
Tag,Top,WhatsThisHelpID,Width
function set(cPropName, xValue)
cPropName = '_'+cPropName
do case
case type('ALen(xValue)')=='N'
* es un arreglo
local nLen,cmd,i
this.addProperty(cPropName+'(1)')
nLen = alen(xValue)
cmd = 'Dimension This.'+cPropName+ ' [ '+Str(nLen,10,0)+']'
&cmd.
for i=1 to nLen
cmd = 'This.'+cPropName+ ' [ '+Str(i,10,0)+'] = xValue[i]'
&cmd.
next
case type('this.'+cPropName)=='U'
* la propiedad no existe, definirla
this.addProperty(cPropName,@xValue)
otherwise
* actualizar la propiedad
local cmd
cmd = 'this.'+cPropName+'=xValue'
&cmd
endcase
return
procedure get (cPropName)
cPropName = '_'+cPropName
If type('this.'+cPropName)=='U'
return ''
Else
local cmd
cmd = 'return this.'+cPropName
&cmd
endif
return ''
enddefine
function testJsonClass
clear
set decimal to 10
oJson = newObject('json')
? 'Test Basic Types'
? '----------------'
? oJson.decode('null')
? oJson.decode('true')
? oJson.decode('false')
?
? oJson.decode('17311')
? oJson.decode('728.45')
? oJson.decode('88.45.')
? oJson.decode('"nacho gtz"')
if not empty(oJson.cError)
? oJson.cError
return
endif
? oJson.decode('"nacho gtz\nEs \"bueno\"\nMuy Bueno\ba"')
if not empty(oJson.cError)
? oJson.cError
return
endif
? 'Test Array'
? '----------'
arr = oJson.decode('[3.1416,"Ignacio",false,null]')
? arr.get(1), arr.get(2), arr.get(3), arr.get(4)
arr = oJson.decode('[ ["Hugo","Paco","Luis"] , [ 8,9,11] ] ')
nombres = arr.get(1)
edades = arr.get(2)
? nombres.get(1), edades.get(1)
? nombres.get(2), edades.get(2)
? nombres.get(3), edades.get(3)
?
? 'Test Object'
? '-----------'
obj = oJson.decode('{"nombre":"Ignacio", "edad":33.17, "isGood":true}')
? obj.get('nombre'), obj.get('edad'), obj.get('isGood')
? obj._Nombre, obj._Edad, obj._IsGood
obj = oJson.decode('{"jsonrpc":"1.0", "id":1, "method":"sumArray", "params":[3.1415,2.14,10],"version":1.0}')
? obj.get('jsonrpc'), obj._jsonrpc
? obj.get('id'), obj._id
? obj.get('method'), obj._method
? obj._Params.array[1], obj._Params.get(1)
?
? 'Test nested object'
? '------------------'
cJson = '{"jsonrpc":"1.0", "id":1, "method":"upload", "params": {"data":{ "usrkey":"2415af77b", "sendto":"[email protected]", "name":"Ignacio is \"Nacho\"","expires":"20120731" }}}'
obj = oJson.decode(cJson)
if not empty(oJson.cError)
? oJson.cError
return
endif
? cJson
? 'method -->',obj._method
? 'usrkey -->',obj._params._data._usrkey
? 'sendto -->',obj._params._data._sendto
? 'name --->',obj._params._data._name
? 'expires ->',obj._params._data._expires
?
? 'Test empty object'
? '-----------------'
cJson = '{"result":null,"error":{"code":-3200.012,"message":"invalid usrkey","data":{}},"id":"1"}'
obj = oJson.decode(cJson)
if not empty(oJson.cError)
? oJson.cError
return
endif
? cJson
? 'result -->',obj._result, obj.get('result')
oError = obj.get('error')
? 'ErrCode ->',obj._error._code, oError.get('code')
? 'ErrMsg -->',obj._error._message, oError.get('message')
? 'id ----->',obj._id, obj.get('id')
? type("oError._code")
?
? 'Probar decode-enconde-decode-encode'
? '------------------------------------'
cJson = ' {"server":"", "user":"", "password":"" ,'+;
' "port":0, "auth":false, "ssl":false, "timeout":20, "error":404}'
? cJson
oSmtp = json_decode(cJson)
cJson = json_encode(oSmtp)
? cJson
oSmtp = json_decode(cJson)
cJson = json_encode(oSmtp)
? cJson
* Probar falla
?
? 'Probar una falla en el json'
? '---------------------------'
cJson = ' {"server":"", "user":"", "password":"" ,'
oSmtp = json_decode(cJson)
if not empty(json_getErrorMsg())
? json_getErrorMsg()
endif
?
? 'Pruebas Finalizadas'
return