@@ -7,7 +7,7 @@ VALUE rb_eFastJsonparserUnknownError, rb_eFastJsonparserParseError;
7
7
using namespace simdjson ;
8
8
9
9
// Convert tape to Ruby's Object
10
- static VALUE make_ruby_object (dom::element element)
10
+ static VALUE make_ruby_object (dom::element element, bool symbolize_names )
11
11
{
12
12
switch (element.type ())
13
13
{
@@ -16,7 +16,7 @@ static VALUE make_ruby_object(dom::element element)
16
16
VALUE ary = rb_ary_new ();
17
17
for (dom::element x : element)
18
18
{
19
- VALUE e = make_ruby_object (x);
19
+ VALUE e = make_ruby_object (x, symbolize_names );
20
20
rb_ary_push (ary, e);
21
21
}
22
22
return ary;
@@ -27,9 +27,12 @@ static VALUE make_ruby_object(dom::element element)
27
27
for (dom::key_value_pair field : dom::object (element))
28
28
{
29
29
std::string_view view (field.key );
30
- VALUE k = rb_intern (view.data ());
31
- VALUE v = make_ruby_object (field.value );
32
- rb_hash_aset (hash, ID2SYM (k), v);
30
+ VALUE k = rb_utf8_str_new (view.data (), view.size ());
31
+ if (symbolize_names) {
32
+ k = ID2SYM (rb_intern_str (k));
33
+ }
34
+ VALUE v = make_ruby_object (field.value , symbolize_names);
35
+ rb_hash_aset (hash, k, v);
33
36
}
34
37
return hash;
35
38
}
@@ -48,7 +51,7 @@ static VALUE make_ruby_object(dom::element element)
48
51
case dom::element_type::STRING:
49
52
{
50
53
std::string_view view (element);
51
- return rb_str_new (view.data (), view.size ());
54
+ return rb_utf8_str_new (view.data (), view.size ());
52
55
}
53
56
case dom::element_type::BOOL:
54
57
{
@@ -63,7 +66,7 @@ static VALUE make_ruby_object(dom::element element)
63
66
rb_raise (rb_eException, " [BUG] must not happen" );
64
67
}
65
68
66
- static VALUE rb_fast_jsonparser_parse (VALUE self, VALUE arg)
69
+ static VALUE rb_fast_jsonparser_parse (VALUE self, VALUE arg, VALUE symbolize_names )
67
70
{
68
71
Check_Type (arg, T_STRING);
69
72
@@ -73,10 +76,10 @@ static VALUE rb_fast_jsonparser_parse(VALUE self, VALUE arg)
73
76
{
74
77
rb_raise (rb_eFastJsonparserParseError, " %s" , error_message (error));
75
78
}
76
- return make_ruby_object (doc);
79
+ return make_ruby_object (doc, RTEST (symbolize_names) );
77
80
}
78
81
79
- static VALUE rb_fast_jsonparser_load (VALUE self, VALUE arg)
82
+ static VALUE rb_fast_jsonparser_load (VALUE self, VALUE arg, VALUE symbolize_names )
80
83
{
81
84
Check_Type (arg, T_STRING);
82
85
@@ -86,10 +89,10 @@ static VALUE rb_fast_jsonparser_load(VALUE self, VALUE arg)
86
89
{
87
90
rb_raise (rb_eFastJsonparserParseError, " %s" , error_message (error));
88
91
}
89
- return make_ruby_object (doc);
92
+ return make_ruby_object (doc, RTEST (symbolize_names) );
90
93
}
91
94
92
- static VALUE rb_fast_jsonparser_load_many (VALUE self, VALUE arg, VALUE batch_size)
95
+ static VALUE rb_fast_jsonparser_load_many (VALUE self, VALUE arg, VALUE symbolize_names, VALUE batch_size)
93
96
{
94
97
Check_Type (arg, T_STRING);
95
98
Check_Type (batch_size, T_FIXNUM);
@@ -104,7 +107,7 @@ static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg, VALUE batch_siz
104
107
105
108
for (dom::element doc : docs)
106
109
{
107
- rb_yield (make_ruby_object (doc));
110
+ rb_yield (make_ruby_object (doc, RTEST (symbolize_names) ));
108
111
}
109
112
110
113
return Qnil;
@@ -120,14 +123,13 @@ extern "C"
120
123
{
121
124
VALUE rb_mFastJsonparser = rb_const_get (rb_cObject, rb_intern (" FastJsonparser" ));
122
125
123
- rb_define_module_function (rb_mFastJsonparser, " parse " , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_parse), 1 );
124
- rb_define_module_function (rb_mFastJsonparser, " load " , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load), 1 );
125
- rb_define_module_function (rb_mFastJsonparser, " _load_many" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load_many), 2 );
126
+ rb_define_module_function (rb_mFastJsonparser, " _parse " , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_parse), 2 );
127
+ rb_define_module_function (rb_mFastJsonparser, " _load " , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load), 2 );
128
+ rb_define_module_function (rb_mFastJsonparser, " _load_many" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load_many), 3 );
126
129
127
130
rb_eFastJsonparserParseError = rb_const_get (rb_mFastJsonparser, rb_intern (" ParseError" ));
128
131
rb_global_variable (&rb_eFastJsonparserParseError);
129
132
rb_eFastJsonparserUnknownError = rb_const_get (rb_mFastJsonparser, rb_intern (" UnknownError" ));
130
133
rb_global_variable (&rb_eFastJsonparserUnknownError);
131
-
132
134
}
133
135
}
0 commit comments