panko_serializer 0.8.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -4
  3. data/lib/panko/array_serializer.rb +55 -16
  4. data/lib/panko/code_gen/active_record/access_classifier.rb +65 -0
  5. data/lib/panko/code_gen/active_record/define_attribute_methods.rb +13 -0
  6. data/lib/panko/code_gen/code_builder.rb +32 -0
  7. data/lib/panko/code_gen/compile_cache.rb +28 -0
  8. data/lib/panko/code_gen/compiler.rb +56 -0
  9. data/lib/panko/code_gen/config.rb +79 -0
  10. data/lib/panko/code_gen/datetime_format.rb +38 -0
  11. data/lib/panko/code_gen/descriptor.rb +136 -0
  12. data/lib/panko/code_gen/descriptor_builder.rb +166 -0
  13. data/lib/panko/code_gen/dump.rb +52 -0
  14. data/lib/panko/code_gen/errors.rb +21 -0
  15. data/lib/panko/code_gen/filter.rb +29 -0
  16. data/lib/panko/code_gen/filter_adapter.rb +44 -0
  17. data/lib/panko/code_gen/filters/indexed.rb +62 -0
  18. data/lib/panko/code_gen/filters/none.rb +19 -0
  19. data/lib/panko/code_gen/generator.rb +23 -0
  20. data/lib/panko/code_gen/generators/banner.rb +27 -0
  21. data/lib/panko/code_gen/generators/class_emitter.rb +120 -0
  22. data/lib/panko/code_gen/generators/cycle_membership.rb +53 -0
  23. data/lib/panko/code_gen/generators/descriptor_walk.rb +22 -0
  24. data/lib/panko/code_gen/generators/fanout.rb +60 -0
  25. data/lib/panko/code_gen/generators/field_index.rb +31 -0
  26. data/lib/panko/code_gen/generators/field_walk.rb +38 -0
  27. data/lib/panko/code_gen/generators/generated_names.rb +73 -0
  28. data/lib/panko/code_gen/generators/hash_sink.rb +217 -0
  29. data/lib/panko/code_gen/generators/json_sink.rb +290 -0
  30. data/lib/panko/code_gen/generators/record_access/generic.rb +92 -0
  31. data/lib/panko/code_gen/generators/record_access/specialized.rb +90 -0
  32. data/lib/panko/code_gen/generators/release.rb +60 -0
  33. data/lib/panko/code_gen/generators/sink.rb +80 -0
  34. data/lib/panko/code_gen/instance_pool.rb +27 -0
  35. data/lib/panko/code_gen/runtime.rb +27 -0
  36. data/lib/panko/code_gen/serializer_cache.rb +242 -0
  37. data/lib/panko/code_gen/validators/callable_arity.rb +42 -0
  38. data/lib/panko/code_gen/validators/name_uniqueness.rb +44 -0
  39. data/lib/panko/code_gen/validators/source_resolution.rb +48 -0
  40. data/lib/panko/code_gen/validators/validator.rb +19 -0
  41. data/lib/panko/code_gen/writers_pool.rb +51 -0
  42. data/lib/panko/code_gen.rb +82 -0
  43. data/lib/panko/compile_all.rb +80 -0
  44. data/lib/panko/compile_all_result.rb +21 -0
  45. data/lib/panko/config.rb +71 -0
  46. data/lib/panko/descriptor.rb +93 -0
  47. data/lib/panko/response.rb +40 -8
  48. data/lib/panko/serializer.rb +175 -91
  49. data/lib/panko/serializer_resolver.rb +2 -8
  50. data/lib/panko/version.rb +1 -1
  51. data/lib/panko_serializer.rb +3 -5
  52. metadata +49 -35
  53. data/ext/panko_serializer/attributes_writer/active_record.c +0 -181
  54. data/ext/panko_serializer/attributes_writer/active_record.h +0 -17
  55. data/ext/panko_serializer/attributes_writer/attributes_writer.c +0 -55
  56. data/ext/panko_serializer/attributes_writer/attributes_writer.h +0 -35
  57. data/ext/panko_serializer/attributes_writer/common.c +0 -10
  58. data/ext/panko_serializer/attributes_writer/common.h +0 -9
  59. data/ext/panko_serializer/attributes_writer/hash.c +0 -13
  60. data/ext/panko_serializer/attributes_writer/hash.h +0 -7
  61. data/ext/panko_serializer/attributes_writer/plain.c +0 -13
  62. data/ext/panko_serializer/attributes_writer/plain.h +0 -7
  63. data/ext/panko_serializer/attributes_writer/type_cast/time_conversion.c +0 -156
  64. data/ext/panko_serializer/attributes_writer/type_cast/time_conversion.h +0 -10
  65. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +0 -409
  66. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.h +0 -81
  67. data/ext/panko_serializer/common.h +0 -9
  68. data/ext/panko_serializer/extconf.rb +0 -28
  69. data/ext/panko_serializer/panko_serializer.c +0 -181
  70. data/ext/panko_serializer/panko_serializer.h +0 -12
  71. data/ext/panko_serializer/serialization_descriptor/association.c +0 -90
  72. data/ext/panko_serializer/serialization_descriptor/association.h +0 -20
  73. data/ext/panko_serializer/serialization_descriptor/attribute.c +0 -96
  74. data/ext/panko_serializer/serialization_descriptor/attribute.h +0 -27
  75. data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.c +0 -177
  76. data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.h +0 -30
  77. data/lib/panko/association.rb +0 -17
  78. data/lib/panko/attribute.rb +0 -29
  79. data/lib/panko/object_writer.rb +0 -68
  80. data/lib/panko/serialization_descriptor.rb +0 -180
@@ -1,12 +0,0 @@
1
- #include <ruby.h>
2
-
3
- #include "attributes_writer/attributes_writer.h"
4
- #include "serialization_descriptor/association.h"
5
- #include "serialization_descriptor/attribute.h"
6
- #include "serialization_descriptor/serialization_descriptor.h"
7
-
8
- VALUE serialize_object(VALUE key, VALUE object, VALUE str_writer,
9
- SerializationDescriptor descriptor);
10
-
11
- VALUE serialize_objects(VALUE key, VALUE objects, VALUE str_writer,
12
- SerializationDescriptor descriptor);
@@ -1,90 +0,0 @@
1
- #include "association.h"
2
-
3
- VALUE cAssociation;
4
-
5
- static void association_free(void* ptr) {
6
- if (!ptr) {
7
- return;
8
- }
9
-
10
- Association association = (Association)ptr;
11
- association->name_str = Qnil;
12
- association->name_id = 0;
13
- association->name_sym = Qnil;
14
- association->rb_descriptor = Qnil;
15
-
16
- if (!association->descriptor || association->descriptor != NULL) {
17
- association->descriptor = NULL;
18
- }
19
-
20
- xfree(association);
21
- }
22
-
23
- void association_mark(Association data) {
24
- rb_gc_mark(data->name_str);
25
- rb_gc_mark(data->name_sym);
26
- rb_gc_mark(data->rb_descriptor);
27
-
28
- if (data->descriptor != NULL) {
29
- sd_mark(data->descriptor);
30
- }
31
- }
32
-
33
- static VALUE association_new(int argc, VALUE* argv, VALUE self) {
34
- Association association;
35
-
36
- Check_Type(argv[0], T_SYMBOL);
37
- Check_Type(argv[1], T_STRING);
38
-
39
- association = ALLOC(struct _Association);
40
- association->name_sym = argv[0];
41
- association->name_str = argv[1];
42
- association->rb_descriptor = argv[2];
43
-
44
- association->name_id = rb_intern_str(rb_sym2str(association->name_sym));
45
- association->descriptor = sd_read(association->rb_descriptor);
46
-
47
- return Data_Wrap_Struct(cAssociation, association_mark, association_free,
48
- association);
49
- }
50
-
51
- Association association_read(VALUE association) {
52
- return (Association)DATA_PTR(association);
53
- }
54
-
55
- VALUE association_name_sym_ref(VALUE self) {
56
- Association association = (Association)DATA_PTR(self);
57
- return association->name_sym;
58
- }
59
-
60
- VALUE association_name_str_ref(VALUE self) {
61
- Association association = (Association)DATA_PTR(self);
62
- return association->name_str;
63
- }
64
-
65
- VALUE association_descriptor_ref(VALUE self) {
66
- Association association = (Association)DATA_PTR(self);
67
- return association->rb_descriptor;
68
- }
69
-
70
- VALUE association_decriptor_aset(VALUE self, VALUE descriptor) {
71
- Association association = (Association)DATA_PTR(self);
72
-
73
- association->rb_descriptor = descriptor;
74
- association->descriptor = sd_read(descriptor);
75
-
76
- return association->rb_descriptor;
77
- }
78
-
79
- void panko_init_association(VALUE mPanko) {
80
- cAssociation = rb_define_class_under(mPanko, "Association", rb_cObject);
81
- rb_undef_alloc_func(cAssociation);
82
- rb_global_variable(&cAssociation);
83
-
84
- rb_define_module_function(cAssociation, "new", association_new, -1);
85
-
86
- rb_define_method(cAssociation, "name_sym", association_name_sym_ref, 0);
87
- rb_define_method(cAssociation, "name_str", association_name_str_ref, 0);
88
- rb_define_method(cAssociation, "descriptor", association_descriptor_ref, 0);
89
- rb_define_method(cAssociation, "descriptor=", association_decriptor_aset, 1);
90
- }
@@ -1,20 +0,0 @@
1
- #include <ruby.h>
2
-
3
- #ifndef __ASSOCIATION_H__
4
- #define __ASSOCIATION_H__
5
-
6
- #include "serialization_descriptor.h"
7
-
8
- typedef struct _Association {
9
- ID name_id;
10
- VALUE name_sym;
11
- VALUE name_str;
12
-
13
- VALUE rb_descriptor;
14
- SerializationDescriptor descriptor;
15
- }* Association;
16
-
17
- Association association_read(VALUE association);
18
- void panko_init_association(VALUE mPanko);
19
-
20
- #endif
@@ -1,96 +0,0 @@
1
- #include "attribute.h"
2
-
3
- ID attribute_aliases_id = 0;
4
- VALUE cAttribute;
5
-
6
- static void attribute_free(void* ptr) {
7
- if (!ptr) {
8
- return;
9
- }
10
-
11
- Attribute attribute = (Attribute)ptr;
12
- attribute->name_str = Qnil;
13
- attribute->name_id = 0;
14
- attribute->alias_name = Qnil;
15
- attribute->type = Qnil;
16
- attribute->record_class = Qnil;
17
-
18
- xfree(attribute);
19
- }
20
-
21
- void attribute_mark(Attribute data) {
22
- rb_gc_mark(data->name_str);
23
- rb_gc_mark(data->alias_name);
24
- rb_gc_mark(data->type);
25
- rb_gc_mark(data->record_class);
26
- }
27
-
28
- static VALUE attribute_new(int argc, VALUE* argv, VALUE self) {
29
- Attribute attribute;
30
-
31
- Check_Type(argv[0], T_STRING);
32
- if (argv[1] != Qnil) {
33
- Check_Type(argv[1], T_STRING);
34
- }
35
-
36
- attribute = ALLOC(struct _Attribute);
37
- attribute->name_str = argv[0];
38
- attribute->name_id = rb_intern_str(attribute->name_str);
39
- attribute->alias_name = argv[1];
40
- attribute->type = Qnil;
41
- attribute->record_class = Qnil;
42
-
43
- return Data_Wrap_Struct(cAttribute, attribute_mark, attribute_free,
44
- attribute);
45
- }
46
-
47
- Attribute attribute_read(VALUE attribute) {
48
- return (Attribute)DATA_PTR(attribute);
49
- }
50
-
51
- void attribute_try_invalidate(Attribute attribute, VALUE new_record_class) {
52
- if (attribute->record_class != new_record_class) {
53
- attribute->type = Qnil;
54
- attribute->record_class = new_record_class;
55
-
56
- // Once the record class is changed for this attribute, check if
57
- // we attribute_aliases (from ActivRecord), if so fill in
58
- // performance wise - this code should be called once (unless the serialzier
59
- // is polymorphic)
60
- volatile VALUE ar_aliases_hash =
61
- rb_funcall(new_record_class, attribute_aliases_id, 0);
62
-
63
- if (!PANKO_EMPTY_HASH(ar_aliases_hash)) {
64
- volatile VALUE aliasedValue =
65
- rb_hash_aref(ar_aliases_hash, attribute->name_str);
66
- if (aliasedValue != Qnil) {
67
- attribute->alias_name = attribute->name_str;
68
- attribute->name_str = aliasedValue;
69
- attribute->name_id = rb_intern_str(attribute->name_str);
70
- }
71
- }
72
- }
73
- }
74
-
75
- VALUE attribute_name_ref(VALUE self) {
76
- Attribute attribute = (Attribute)DATA_PTR(self);
77
- return attribute->name_str;
78
- }
79
-
80
- VALUE attribute_alias_name_ref(VALUE self) {
81
- Attribute attribute = (Attribute)DATA_PTR(self);
82
- return attribute->alias_name;
83
- }
84
-
85
- void panko_init_attribute(VALUE mPanko) {
86
- attribute_aliases_id = rb_intern("attribute_aliases");
87
-
88
- cAttribute = rb_define_class_under(mPanko, "Attribute", rb_cObject);
89
- rb_undef_alloc_func(cAttribute);
90
- rb_global_variable(&cAttribute);
91
-
92
- rb_define_module_function(cAttribute, "new", attribute_new, -1);
93
-
94
- rb_define_method(cAttribute, "name", attribute_name_ref, 0);
95
- rb_define_method(cAttribute, "alias_name", attribute_alias_name_ref, 0);
96
- }
@@ -1,27 +0,0 @@
1
- #include <ruby.h>
2
-
3
- #ifndef __ATTRIBUTE_H__
4
- #define __ATTRIBUTE_H__
5
-
6
- #include "../common.h"
7
-
8
- typedef struct _Attribute {
9
- VALUE name_str;
10
- ID name_id;
11
- VALUE alias_name;
12
-
13
- /*
14
- * We will cache the activerecord type
15
- * by the record_class
16
- */
17
- VALUE type;
18
- VALUE record_class;
19
- }* Attribute;
20
-
21
- Attribute attribute_read(VALUE attribute);
22
- void attribute_try_invalidate(Attribute attribute, VALUE new_record_class);
23
- void panko_init_attribute(VALUE mPanko);
24
-
25
- #define PANKO_ATTRIBUTE_READ(attribute) (Attribute) DATA_PTR(attribute)
26
-
27
- #endif
@@ -1,177 +0,0 @@
1
- #include "serialization_descriptor.h"
2
-
3
- static ID object_id;
4
- static ID sc_id;
5
-
6
- static void sd_free(SerializationDescriptor sd) {
7
- if (!sd) {
8
- return;
9
- }
10
-
11
- sd->serializer = Qnil;
12
- sd->serializer_type = Qnil;
13
- sd->attributes = Qnil;
14
- sd->method_fields = Qnil;
15
- sd->has_one_associations = Qnil;
16
- sd->has_many_associations = Qnil;
17
- sd->aliases = Qnil;
18
- xfree(sd);
19
- }
20
-
21
- void sd_mark(SerializationDescriptor data) {
22
- rb_gc_mark(data->serializer);
23
- rb_gc_mark(data->serializer_type);
24
- rb_gc_mark(data->attributes);
25
- rb_gc_mark(data->method_fields);
26
- rb_gc_mark(data->has_one_associations);
27
- rb_gc_mark(data->has_many_associations);
28
- rb_gc_mark(data->aliases);
29
- }
30
-
31
- static VALUE sd_alloc(VALUE klass) {
32
- SerializationDescriptor sd = ALLOC(struct _SerializationDescriptor);
33
-
34
- sd->serializer = Qnil;
35
- sd->serializer_type = Qnil;
36
- sd->attributes = Qnil;
37
- sd->method_fields = Qnil;
38
- sd->has_one_associations = Qnil;
39
- sd->has_many_associations = Qnil;
40
- sd->aliases = Qnil;
41
-
42
- sd->attributes_writer = create_empty_attributes_writer();
43
-
44
- return Data_Wrap_Struct(klass, sd_mark, sd_free, sd);
45
- }
46
-
47
- SerializationDescriptor sd_read(VALUE descriptor) {
48
- return (SerializationDescriptor)DATA_PTR(descriptor);
49
- }
50
-
51
- void sd_set_writer(SerializationDescriptor sd, VALUE object) {
52
- if (sd->attributes_writer.object_type != UnknownObjectType) {
53
- return;
54
- }
55
-
56
- sd->attributes_writer = create_attributes_writer(object);
57
- }
58
-
59
- VALUE sd_serializer_set(VALUE self, VALUE serializer) {
60
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
61
-
62
- sd->serializer = serializer;
63
- return Qnil;
64
- }
65
-
66
- VALUE sd_serializer_ref(VALUE self) {
67
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
68
-
69
- return sd->serializer;
70
- }
71
-
72
- VALUE sd_attributes_set(VALUE self, VALUE attributes) {
73
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
74
-
75
- sd->attributes = attributes;
76
- return Qnil;
77
- }
78
-
79
- VALUE sd_attributes_ref(VALUE self) {
80
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
81
- return sd->attributes;
82
- }
83
-
84
- VALUE sd_method_fields_set(VALUE self, VALUE method_fields) {
85
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
86
- sd->method_fields = method_fields;
87
- return Qnil;
88
- }
89
-
90
- VALUE sd_method_fields_ref(VALUE self) {
91
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
92
- return sd->method_fields;
93
- }
94
-
95
- VALUE sd_has_one_associations_set(VALUE self, VALUE has_one_associations) {
96
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
97
- sd->has_one_associations = has_one_associations;
98
- return Qnil;
99
- }
100
-
101
- VALUE sd_has_one_associations_ref(VALUE self) {
102
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
103
- return sd->has_one_associations;
104
- }
105
-
106
- VALUE sd_has_many_associations_set(VALUE self, VALUE has_many_associations) {
107
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
108
- sd->has_many_associations = has_many_associations;
109
- return Qnil;
110
- }
111
-
112
- VALUE sd_has_many_associations_ref(VALUE self) {
113
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
114
- return sd->has_many_associations;
115
- }
116
-
117
- VALUE sd_type_set(VALUE self, VALUE type) {
118
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
119
- sd->serializer_type = type;
120
- return Qnil;
121
- }
122
-
123
- VALUE sd_type_aref(VALUE self) {
124
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
125
- return sd->serializer_type;
126
- }
127
-
128
- VALUE sd_aliases_set(VALUE self, VALUE aliases) {
129
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
130
- sd->aliases = aliases;
131
- return Qnil;
132
- }
133
-
134
- VALUE sd_aliases_aref(VALUE self) {
135
- SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
136
- return sd->aliases;
137
- }
138
-
139
- void panko_init_serialization_descriptor(VALUE mPanko) {
140
- object_id = rb_intern("@object");
141
- sc_id = rb_intern("@sc");
142
-
143
- VALUE cSerializationDescriptor =
144
- rb_define_class_under(mPanko, "SerializationDescriptor", rb_cObject);
145
-
146
- rb_define_alloc_func(cSerializationDescriptor, sd_alloc);
147
- rb_define_method(cSerializationDescriptor, "serializer=", sd_serializer_set,
148
- 1);
149
- rb_define_method(cSerializationDescriptor, "serializer", sd_serializer_ref,
150
- 0);
151
-
152
- rb_define_method(cSerializationDescriptor, "attributes=", sd_attributes_set,
153
- 1);
154
- rb_define_method(cSerializationDescriptor, "attributes", sd_attributes_ref,
155
- 0);
156
-
157
- rb_define_method(cSerializationDescriptor,
158
- "method_fields=", sd_method_fields_set, 1);
159
- rb_define_method(cSerializationDescriptor, "method_fields",
160
- sd_method_fields_ref, 0);
161
-
162
- rb_define_method(cSerializationDescriptor,
163
- "has_one_associations=", sd_has_one_associations_set, 1);
164
- rb_define_method(cSerializationDescriptor, "has_one_associations",
165
- sd_has_one_associations_ref, 0);
166
-
167
- rb_define_method(cSerializationDescriptor,
168
- "has_many_associations=", sd_has_many_associations_set, 1);
169
- rb_define_method(cSerializationDescriptor, "has_many_associations",
170
- sd_has_many_associations_ref, 0);
171
-
172
- rb_define_method(cSerializationDescriptor, "type=", sd_type_set, 1);
173
- rb_define_method(cSerializationDescriptor, "type", sd_type_aref, 0);
174
-
175
- rb_define_method(cSerializationDescriptor, "aliases=", sd_aliases_set, 1);
176
- rb_define_method(cSerializationDescriptor, "aliases", sd_aliases_aref, 0);
177
- }
@@ -1,30 +0,0 @@
1
- #pragma once
2
-
3
- #include <ruby.h>
4
- #include <stdbool.h>
5
-
6
- #include "attributes_writer/attributes_writer.h"
7
-
8
- typedef struct _SerializationDescriptor {
9
- // type of the serializer, so we can create it later
10
- VALUE serializer_type;
11
- // Cached value of the serializer
12
- VALUE serializer;
13
-
14
- // Metadata
15
- VALUE attributes;
16
- VALUE aliases;
17
- VALUE method_fields;
18
- VALUE has_one_associations;
19
- VALUE has_many_associations;
20
-
21
- AttributesWriter attributes_writer;
22
- }* SerializationDescriptor;
23
-
24
- SerializationDescriptor sd_read(VALUE descriptor);
25
-
26
- void sd_mark(SerializationDescriptor data);
27
-
28
- void sd_set_writer(SerializationDescriptor sd, VALUE object);
29
-
30
- void panko_init_serialization_descriptor(VALUE mPanko);
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Panko
4
- class Association
5
- def duplicate
6
- Panko::Association.new(
7
- name_sym,
8
- name_str,
9
- Panko::SerializationDescriptor.duplicate(descriptor)
10
- )
11
- end
12
-
13
- def inspect
14
- "<Panko::Association name=#{name_str.inspect}>"
15
- end
16
- end
17
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Panko
4
- class Attribute
5
- def self.create(name, alias_name: nil)
6
- alias_name = alias_name.to_s unless alias_name.nil?
7
- Attribute.new(name.to_s, alias_name)
8
- end
9
-
10
- def ==(other)
11
- return name.to_sym == other if other.is_a? Symbol
12
- return name == other.name && alias_name == other.alias_name if other.is_a? Panko::Attribute
13
-
14
- super
15
- end
16
-
17
- def hash
18
- name.to_sym.hash
19
- end
20
-
21
- def eql?(other)
22
- self.==(other)
23
- end
24
-
25
- def inspect
26
- "<Panko::Attribute name=#{name.inspect} alias_name=#{alias_name.inspect}>"
27
- end
28
- end
29
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Panko::ObjectWriter
4
- def initialize
5
- @values = []
6
- @keys = []
7
-
8
- @next_key = nil
9
- @output = nil
10
- end
11
-
12
- def push_object(key = nil)
13
- @values << {}
14
- @keys << key
15
- end
16
-
17
- def push_array(key = nil)
18
- @values << []
19
- @keys << key
20
- end
21
-
22
- def push_key(key)
23
- @next_key = key
24
- end
25
-
26
- def push_value(value, key = nil)
27
- unless @next_key.nil?
28
- raise "push_value is called with key after push_key is called" unless key.nil?
29
- key = @next_key
30
- @next_key = nil
31
- end
32
-
33
- @values.last[key] = value.as_json
34
- end
35
-
36
- def push_json(value, key = nil)
37
- if value.is_a?(String)
38
- value = begin
39
- Oj.load(value)
40
- rescue
41
- nil
42
- end
43
- end
44
-
45
- push_value(value, key)
46
- end
47
-
48
- def pop
49
- result = @values.pop
50
-
51
- if @values.empty?
52
- @output = result
53
- return
54
- end
55
-
56
- scope_key = @keys.pop
57
- if scope_key.nil?
58
- @values.last << result
59
- else
60
- @values.last[scope_key] = result
61
- end
62
- end
63
-
64
- def output
65
- raise "Output is called before poping all" unless @values.empty?
66
- @output
67
- end
68
- end