-
Notifications
You must be signed in to change notification settings - Fork 38
/
json_serializer.c
799 lines (674 loc) · 21.4 KB
/
json_serializer.c
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#include "protocolbuffers.h"
#include "serializer.h"
#include "json_serializer.h"
typedef struct {
int (*serialize_double)(double value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_float)(float value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_int64)(int64_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_uint64)(uint64_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_int32)(int32_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_fixed64)(uint64_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_fixed32)(int32_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_bool)(zend_bool value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_string)(const char *value, size_t value_len, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_group)(void);
int (*serialize_bytes)(void *value, size_t value_len, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_uint32)(uint32_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_enum)(int32_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_sfixed32)(int32_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_sfixed64)(int64_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_sint32)(int32_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_sint64)(int64_t value, php_protocolbuffers_scheme *scheme, php_protocolbuffers_scheme_container *container, void *opaque TSRMLS_DC);
int (*serialize_message)(zval *value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *child_container,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC);
int (*serialize_repeated_begin)(
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC);
int (*serialize_repeated_end)(zval *value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC);
} php_protocolbuffers_serializer2;
static int _json_serializer_float(
float value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_double(result, value);
} else {
add_assoc_double_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_double(
double value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_double(result, value);
} else {
add_assoc_double_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_repeated_begin(
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval **result = (zval**)opaque;
zval *outer = NULL;
MAKE_STD_ZVAL(outer);
array_init(outer);
*result = outer;
return 0;
}
static int _json_serializer_repeated_end(
zval *value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *outer = (zval*)opaque;
add_assoc_zval_ex(value, scheme->original_name, scheme->original_name_len, outer);
return 0;
}
static int _json_serializer_enum(
int32_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_sint32(
int32_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_sint64(
int64_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_fixed64(
uint64_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_sfixed32(
int32_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_sfixed64(
int64_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_fixed32(
int32_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_uint32(
uint32_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_bool(
zend_bool value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_bool(result, value);
} else {
add_assoc_bool_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_int64(
int64_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_uint64(
uint64_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_int32(
int32_t value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC
)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_long(result, value);
} else {
add_assoc_long_ex(result, scheme->original_name, scheme->original_name_len, value);
}
return 0;
}
static int _json_serializer_bytes(
void *value,
size_t value_len,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_stringl(result, value, value_len, 1);
} else {
add_assoc_stringl_ex(result, scheme->original_name, scheme->original_name_len, value, value_len, 1);
}
return 0;
}
static int _json_serializer_string(
const char *value,
size_t value_len,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC)
{
zval *result = (zval*)opaque;
if (scheme->repeated) {
add_next_index_stringl(result, value, value_len, 1);
} else {
add_assoc_stringl_ex(result, scheme->original_name, scheme->original_name_len, (char*)value, value_len, 1);
}
return 0;
}
static int _json_serializer_message(
zval *value,
php_protocolbuffers_scheme *scheme,
php_protocolbuffers_scheme_container *child_container,
php_protocolbuffers_scheme_container *container,
void *opaque TSRMLS_DC) {
zval *result = (zval*)opaque;
zval *tmp;
MAKE_STD_ZVAL(tmp);
array_init(tmp);
php_protocolbuffers_encode_jsonserialize(value, child_container, 0, &tmp TSRMLS_CC);
if (scheme->repeated) {
add_next_index_zval(result, tmp);
} else {
add_assoc_zval_ex(result, scheme->original_name, scheme->original_name_len, tmp);
}
return 0;
}
static php_protocolbuffers_serializer2 json_serializer = {
_json_serializer_double,//double
_json_serializer_float,//float
_json_serializer_int64,//int64
_json_serializer_uint64,//uint64
_json_serializer_int32,//int32
_json_serializer_fixed64,//fixed64
_json_serializer_fixed32,//fixed32
_json_serializer_bool,//bool
_json_serializer_string,//string
NULL,//group
_json_serializer_bytes,//bytes
_json_serializer_uint32,//uint32
_json_serializer_enum,//enum
_json_serializer_sfixed32,//sfixed32
_json_serializer_sfixed64,//sfixed64
_json_serializer_sint32,//sint32
_json_serializer_sint64,//sint64
_json_serializer_message,//message
_json_serializer_repeated_begin, //repeated_begin
_json_serializer_repeated_end, //repeated_end
};
static const char* php_protocolbuffers_get_property_name(php_protocolbuffers_scheme_container *container, php_protocolbuffers_scheme *scheme, int *name_len)
{
const char *name;
if (container->use_single_property < 1) {
name = scheme->mangled_name;
*name_len = scheme->mangled_name_len;
} else {
name = scheme->name;
*name_len = scheme->name_len;
}
return name;
}
static int convert_to_int64(zval *value, int64_t *result)
{
int64_t v = 0;
if (Z_TYPE_P(value) != IS_LONG) {
if (Z_TYPE_P(value) == IS_STRING) {
v = (int64_t)atoll(Z_STRVAL_P(value));
} else {
convert_to_long(value);
v = (int64_t)Z_LVAL_P(value);
}
} else {
v = (int64_t)Z_LVAL_P(value);
}
*result = v;
return 0;
}
static int convert_to_uint64(zval *value, uint64_t *result)
{
uint64_t v = 0;
if (Z_TYPE_P(value) != IS_LONG) {
if (Z_TYPE_P(value) == IS_STRING) {
char *endptr = NULL;
errno = 0;
v = strtoull(Z_STRVAL_P(value), &endptr, 0);
if (errno == ERANGE) {
zend_error(E_WARNING, "php_protocolbuffers_encode_value_fixed64: strtoull failed to decode string");
return -1;
}
} else if (Z_TYPE_P(value) == IS_DOUBLE) {
v = (uint64_t)Z_DVAL_P(value);
} else {
convert_to_long(value);
v = Z_LVAL_P(value);
}
} else {
v = Z_LVAL_P(value);
}
*result = v;
return 0;
}
static int convert_to_uint32(zval *value, uint32_t *result)
{
uint32_t v = 0;
if (Z_TYPE_P(value) != IS_LONG) {
if (Z_TYPE_P(value) == IS_STRING) {
v = (uint32_t)atol(Z_STRVAL_P(value));
} else {
convert_to_long(value);
v = (uint32_t)Z_LVAL_P(value);
}
} else {
v = (uint32_t)Z_LVAL_P(value);
}
*result = v;
return 0;
}
static int convert_to_int32(zval *value, int32_t *result)
{
int32_t v = 0;
if (Z_TYPE_P(value) != IS_LONG) {
if (Z_TYPE_P(value) == IS_STRING) {
v = (int32_t)atol(Z_STRVAL_P(value));
} else {
convert_to_long(value);
v = (int32_t)Z_LVAL_P(value);
}
} else {
v = (int32_t)Z_LVAL_P(value);
}
*result = v;
return 0;
}
static int php_protocolbuffers_json_encode_value(zval **element, php_protocolbuffers_scheme_container *container, php_protocolbuffers_scheme *scheme, php_protocolbuffers_serializer2 *ser, int throws_exception, void *opaque TSRMLS_DC)
{
zval value_copy;
zval *outer = (zval*)opaque;
value_copy = **element;
switch (scheme->type) {
case TYPE_DOUBLE:
zval_copy_ctor(&value_copy);
convert_to_double(&value_copy);
ser->serialize_double((double)Z_DVAL_P(&value_copy), scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
case TYPE_FLOAT:
zval_copy_ctor(&value_copy);
convert_to_double(&value_copy);
ser->serialize_float((float)Z_DVAL_P(&value_copy), scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
case TYPE_INT64:
{
int64_t v;
zval_copy_ctor(&value_copy);
convert_to_int64(&value_copy, &v);
ser->serialize_int64(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_UINT64:
{
uint64_t v;
zval_copy_ctor(&value_copy);
convert_to_uint64(&value_copy, &v);
ser->serialize_uint64(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_INT32:
{
int32_t v;
zval_copy_ctor(&value_copy);
convert_to_int32(&value_copy, &v);
ser->serialize_int32(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_FIXED64:
{
uint64_t v;
zval_copy_ctor(&value_copy);
convert_to_uint64(&value_copy, &v);
ser->serialize_fixed64(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_FIXED32:
{
uint32_t v;
zval_copy_ctor(&value_copy);
convert_to_uint32(&value_copy, &v);
ser->serialize_fixed32(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_BOOL:
zval_copy_ctor(&value_copy);
convert_to_boolean(&value_copy);
ser->serialize_bool(Z_BVAL_P(&value_copy), scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
case TYPE_STRING:
if (Z_STRLEN_PP(element) == 0 || (Z_STRLEN_PP(element) == 1 && Z_STRVAL_PP(element)[0] == '0')) {
return -1;
}
if (PBG(validate_string) && is_utf8(Z_STRVAL_PP(element), Z_STRLEN_PP(element)) < 1) {
php_protocolbuffers_raise_error_or_exception(php_protocol_buffers_invalid_byte_sequence_class_entry, E_WARNING, throws_exception, "passed string is not valid utf8 string");
return -1;
}
ser->serialize_string(Z_STRVAL_PP(element), Z_STRLEN_PP(element), scheme, container, outer TSRMLS_CC);
break;
case TYPE_GROUP:
break;
case TYPE_MESSAGE: {
php_protocolbuffers_scheme_container *child_scheme;
php_protocolbuffers_get_scheme_container(scheme->ce->name, scheme->ce->name_length, &child_scheme TSRMLS_CC);
ser->serialize_message(*element, scheme, child_scheme, container, outer TSRMLS_CC);
break;
}
case TYPE_BYTES:
if (Z_STRLEN_PP(element) == 0 || (Z_STRLEN_PP(element) == 1 && Z_STRVAL_PP(element)[0] == '0')) {
return -1;
}
ser->serialize_bytes(Z_STRVAL_PP(element), Z_STRLEN_PP(element), scheme, container, outer TSRMLS_CC);
case TYPE_UINT32:
{
uint32_t v;
zval_copy_ctor(&value_copy);
convert_to_uint32(&value_copy, &v);
ser->serialize_uint32(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_ENUM:
zval_copy_ctor(&value_copy);
convert_to_long(&value_copy);
ser->serialize_enum((int32_t)Z_LVAL_P(&value_copy), scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
break;
case TYPE_SFIXED32:
{
int32_t v;
zval_copy_ctor(&value_copy);
convert_to_int32(&value_copy, &v);
ser->serialize_sfixed32(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_SFIXED64:
{
int64_t v;
zval_copy_ctor(&value_copy);
convert_to_int64(&value_copy, &v);
ser->serialize_sfixed64(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_SINT32:
{
int32_t v;
zval_copy_ctor(&value_copy);
convert_to_int32(&value_copy, &v);
ser->serialize_sint32(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
case TYPE_SINT64:
{
int64_t v;
zval_copy_ctor(&value_copy);
convert_to_int64(&value_copy, &v);
ser->serialize_sint64(v, scheme, container, outer TSRMLS_CC);
zval_dtor(&value_copy);
break;
}
break;
default:
break;
}
return 0;
}
static void php_protocolbuffers_json_encode_element(php_protocolbuffers_scheme_container *container, HashTable *hash, php_protocolbuffers_scheme *scheme, int throws_exception, zval *result TSRMLS_DC)
{
zval **tmp = NULL;
const char *name = {0};
int name_len = 0;
php_protocolbuffers_serializer2 *ser = &json_serializer;
name = php_protocolbuffers_get_property_name(container, scheme, &name_len);
if (zend_hash_find(hash, name, name_len, (void **)&tmp) == SUCCESS) {
HashPosition pos;
zval **element;
if (scheme->repeated) {
zval *outer; // TODO(chobie): abstraction
if (Z_TYPE_PP(tmp) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_PP(tmp)) > 0) {
ser->serialize_repeated_begin(scheme, container, (void*)&outer TSRMLS_CC);
for(zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(tmp), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_PP(tmp), (void **)&element, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_PP(tmp), &pos)
) {
if (Z_TYPE_PP(element) == IS_NULL) {
continue;
}
php_protocolbuffers_json_encode_value(element, container, scheme, ser, throws_exception, (void*)outer TSRMLS_CC);
}
ser->serialize_repeated_end(result, scheme, container, outer TSRMLS_CC);
}
} else {
if (scheme->required > 0 && Z_TYPE_PP(tmp) == IS_NULL) {
php_protocolbuffers_raise_error_or_exception(php_protocol_buffers_uninitialized_message_exception_class_entry, E_WARNING, throws_exception, "the class does not have required property `%s`.", scheme->name);
return;
}
if (scheme->required == 0 && Z_TYPE_PP(tmp) == IS_NULL) {
return;
}
if (scheme->ce != NULL && Z_TYPE_PP(tmp) != IS_OBJECT) {
return;
}
if (Z_TYPE_PP(tmp) == IS_ARRAY) {
//php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not repeated field but array given", scheme->name);
return;
}
php_protocolbuffers_json_encode_value(tmp, container, scheme, ser, throws_exception, (void*)result TSRMLS_CC);
}
} else {
if (scheme->required > 0) {
php_protocolbuffers_raise_error_or_exception(php_protocol_buffers_invalid_protocolbuffers_exception_class_entry, E_WARNING, throws_exception, "the class does not declared required property `%s`. probably you missed declaration", scheme->name);
return;
}
}
}
int php_protocolbuffers_fetch_element2(php_protocolbuffers_scheme_container *container, HashTable *hash, php_protocolbuffers_scheme *scheme, int throws_exception, zval **output TSRMLS_DC)
{
zval **tmp = NULL;
char *name = {0};
int name_len = 0;
if (container->use_single_property < 1) {
name = scheme->mangled_name;
name_len = scheme->mangled_name_len;
} else {
name = scheme->name;
name_len = scheme->name_len;
}
if (zend_hash_find(hash, name, name_len, (void **)&tmp) == SUCCESS) {
*output = *tmp;
} else {
if (scheme->required > 0) {
php_protocolbuffers_raise_error_or_exception(php_protocol_buffers_invalid_protocolbuffers_exception_class_entry, E_WARNING, 0, "the class does not declared required property `%s`. probably you missed declaration", scheme->name);
return 1;
}
}
return 0;
}
int php_protocolbuffers_encode_jsonserialize(zval *klass, php_protocolbuffers_scheme_container *container, int throws_exception, zval **result TSRMLS_DC)
{
int i = 0;
php_protocolbuffers_scheme *scheme;
HashTable *hash = NULL;
zval **c = NULL;
zval *target = *result;
if (container->use_single_property < 1) {
hash = Z_OBJPROP_P(klass);
} else {
if (zend_hash_find(Z_OBJPROP_P(klass), container->single_property_name, container->single_property_name_len+1, (void**)&c) == SUCCESS) {
hash = Z_ARRVAL_PP(c);
} else {
php_protocolbuffers_raise_error_or_exception(spl_ce_InvalidArgumentException, E_WARNING, throws_exception, "the class does not have `_properties` protected property.");
return -1;
}
}
for (i = 0; i < container->size; i++) {
zval *tmp;
scheme = &(container->scheme[i]);
if (php_protocolbuffers_fetch_element2(container, hash, scheme, throws_exception, &tmp TSRMLS_CC)) {
return -1;
}
php_protocolbuffers_json_encode_element(container, hash, scheme, throws_exception, target TSRMLS_CC);
if (EG(exception)) {
return 1;
}
}
return 0;
}