Skip to content

Commit 524b949

Browse files
etiennebarriebyroot
andcommitted
Introduce JSON::Coder
Co-authored-by: Jean Boussier <[email protected]>
1 parent edd61b4 commit 524b949

File tree

12 files changed

+338
-21
lines changed

12 files changed

+338
-21
lines changed

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
2929

3030
$ gem install json
3131

32-
## Usage
32+
## Basic Usage
3333

3434
To use JSON you can
3535

@@ -52,9 +52,70 @@ You can also use the `pretty_generate` method (which formats the output more
5252
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
5353
checks generate performs, e. g. nesting deepness checks).
5454

55+
## Casting non native types
56+
57+
JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.
58+
59+
By default if you attempt to serialize something else, `JSON.generate` will
60+
search for a `#to_json` method on that object:
61+
62+
```ruby
63+
Position = Struct.new(:latitude, :longitude) do
64+
def to_json(state = nil, *)
65+
JSON::State.from_state(state).generate({
66+
latitude: latitude,
67+
longitude: longitude,
68+
})
69+
end
70+
end
71+
72+
JSON.generate([
73+
Position.new(12323.234, 435345.233),
74+
Position.new(23434.676, 159435.324),
75+
]) # => [{"latitude":12323.234,"longitude":435345.233},{"latitude":23434.676,"longitude":159435.324}]
76+
```
77+
78+
If a `#to_json` method isn't defined on the object, `JSON.generate` will fallback to call `#to_s`:
79+
80+
```ruby
81+
JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"
82+
```
83+
84+
Both of these behavior can be disabled using the `strict: true` option:
85+
86+
```ruby
87+
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
88+
JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
89+
```
90+
91+
## JSON::Coder
92+
93+
Since `#to_json` methods are global, it can sometimes be problematic if you need a given type to be
94+
serialized in different ways in different locations.
95+
96+
Instead it is recommended to use the newer `JSON::Coder` API:
97+
98+
```ruby
99+
module MyApp
100+
API_JSON_CODER = JSON::Coder.new do |object|
101+
case object
102+
when Time
103+
object.iso8601(3)
104+
else
105+
object
106+
end
107+
end
108+
end
109+
110+
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
111+
```
112+
113+
The provided block is called for all objects that don't have a native JSON equivalent, and
114+
must return a Ruby object that has a native JSON equivalent.
115+
55116
## Combining JSON fragments
56117

57-
To combine JSON fragments to build a bigger JSON document, you can use `JSON::Fragment`:
118+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
58119

59120
```ruby
60121
posts_json = cache.fetch_multi(post_ids) do |post_id|
@@ -64,7 +125,7 @@ posts_json.map { |post_json| JSON::Fragment.new(post_json) }
64125
JSON.generate({ posts: posts_json, count: posts_json.count })
65126
```
66127

67-
## Handling arbitrary types
128+
## Round-tripping arbitrary types
68129

69130
> [!CAUTION]
70131
> You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,

benchmark/encoder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
def implementations(ruby_obj)
1919
state = JSON::State.new(JSON.dump_default_options)
20+
coder = JSON::Coder.new
2021
{
2122
json: ["json", proc { JSON.generate(ruby_obj) }],
23+
json_coder: ["json_coder", proc { coder.dump(ruby_obj) }],
2224
oj: ["oj", proc { Oj.dump(ruby_obj) }],
2325
}
2426
end

benchmark/parser.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
def benchmark_parsing(name, json_output)
1717
puts "== Parsing #{name} (#{json_output.size} bytes)"
18+
coder = JSON::Coder.new
1819

1920
Benchmark.ips do |x|
2021
x.report("json") { JSON.parse(json_output) } if RUN[:json]
22+
x.report("json_coder") { coder.load(json_output) } if RUN[:json_coder]
2123
x.report("oj") { Oj.load(json_output) } if RUN[:oj]
2224
x.report("Oj::Parser") { Oj::Parser.new(:usual).parse(json_output) } if RUN[:oj]
2325
x.report("rapidjson") { RapidJSON.parse(json_output) } if RUN[:rapidjson]

ext/json/ext/generator/generator.c

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct JSON_Generator_StateStruct {
1212
VALUE space_before;
1313
VALUE object_nl;
1414
VALUE array_nl;
15+
VALUE as_json;
1516

1617
long max_nesting;
1718
long depth;
@@ -30,8 +31,8 @@ typedef struct JSON_Generator_StateStruct {
3031
static VALUE mJSON, cState, cFragment, mString_Extend, eGeneratorError, eNestingError, Encoding_UTF_8;
3132

3233
static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
33-
static ID sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
34-
sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict;
34+
static VALUE sym_indent, sym_space, sym_space_before, sym_object_nl, sym_array_nl, sym_max_nesting, sym_allow_nan,
35+
sym_ascii_only, sym_depth, sym_buffer_initial_length, sym_script_safe, sym_escape_slash, sym_strict, sym_as_json;
3536

3637

3738
#define GET_STATE_TO(self, state) \
@@ -648,6 +649,7 @@ static void State_mark(void *ptr)
648649
rb_gc_mark_movable(state->space_before);
649650
rb_gc_mark_movable(state->object_nl);
650651
rb_gc_mark_movable(state->array_nl);
652+
rb_gc_mark_movable(state->as_json);
651653
}
652654

653655
static void State_compact(void *ptr)
@@ -658,6 +660,7 @@ static void State_compact(void *ptr)
658660
state->space_before = rb_gc_location(state->space_before);
659661
state->object_nl = rb_gc_location(state->object_nl);
660662
state->array_nl = rb_gc_location(state->array_nl);
663+
state->as_json = rb_gc_location(state->as_json);
661664
}
662665

663666
static void State_free(void *ptr)
@@ -714,6 +717,7 @@ static void vstate_spill(struct generate_json_data *data)
714717
RB_OBJ_WRITTEN(vstate, Qundef, state->space_before);
715718
RB_OBJ_WRITTEN(vstate, Qundef, state->object_nl);
716719
RB_OBJ_WRITTEN(vstate, Qundef, state->array_nl);
720+
RB_OBJ_WRITTEN(vstate, Qundef, state->as_json);
717721
}
718722

719723
static inline VALUE vstate_get(struct generate_json_data *data)
@@ -982,6 +986,8 @@ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *d
982986
static void generate_json(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
983987
{
984988
VALUE tmp;
989+
bool as_json_called = false;
990+
start:
985991
if (obj == Qnil) {
986992
generate_json_null(buffer, data, state, obj);
987993
} else if (obj == Qfalse) {
@@ -1025,7 +1031,13 @@ static void generate_json(FBuffer *buffer, struct generate_json_data *data, JSON
10251031
default:
10261032
general:
10271033
if (state->strict) {
1028-
raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
1034+
if (RTEST(state->as_json) && !as_json_called) {
1035+
obj = rb_proc_call_with_block(state->as_json, 1, &obj, Qnil);
1036+
as_json_called = true;
1037+
goto start;
1038+
} else {
1039+
raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
1040+
}
10291041
} else if (rb_respond_to(obj, i_to_json)) {
10301042
tmp = rb_funcall(obj, i_to_json, 1, vstate_get(data));
10311043
Check_Type(tmp, T_STRING);
@@ -1126,6 +1138,7 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
11261138
objState->space_before = origState->space_before;
11271139
objState->object_nl = origState->object_nl;
11281140
objState->array_nl = origState->array_nl;
1141+
objState->as_json = origState->as_json;
11291142
return obj;
11301143
}
11311144

@@ -1277,6 +1290,28 @@ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
12771290
return Qnil;
12781291
}
12791292

1293+
/*
1294+
* call-seq: as_json()
1295+
*
1296+
* This string is put at the end of a line that holds a JSON array.
1297+
*/
1298+
static VALUE cState_as_json(VALUE self)
1299+
{
1300+
GET_STATE(self);
1301+
return state->as_json;
1302+
}
1303+
1304+
/*
1305+
* call-seq: as_json=(as_json)
1306+
*
1307+
* This string is put at the end of a line that holds a JSON array.
1308+
*/
1309+
static VALUE cState_as_json_set(VALUE self, VALUE as_json)
1310+
{
1311+
GET_STATE(self);
1312+
RB_OBJ_WRITE(self, &state->as_json, rb_convert_type(as_json, T_DATA, "Proc", "to_proc"));
1313+
return Qnil;
1314+
}
12801315

12811316
/*
12821317
* call-seq: check_circular?
@@ -1498,6 +1533,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
14981533
else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
14991534
else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
15001535
else if (key == sym_strict) { state->strict = RTEST(val); }
1536+
else if (key == sym_as_json) { state->as_json = rb_convert_type(val, T_DATA, "Proc", "to_proc"); }
15011537
return ST_CONTINUE;
15021538
}
15031539

@@ -1589,6 +1625,8 @@ void Init_generator(void)
15891625
rb_define_method(cState, "object_nl=", cState_object_nl_set, 1);
15901626
rb_define_method(cState, "array_nl", cState_array_nl, 0);
15911627
rb_define_method(cState, "array_nl=", cState_array_nl_set, 1);
1628+
rb_define_method(cState, "as_json", cState_as_json, 0);
1629+
rb_define_method(cState, "as_json=", cState_as_json_set, 1);
15921630
rb_define_method(cState, "max_nesting", cState_max_nesting, 0);
15931631
rb_define_method(cState, "max_nesting=", cState_max_nesting_set, 1);
15941632
rb_define_method(cState, "script_safe", cState_script_safe, 0);
@@ -1610,6 +1648,7 @@ void Init_generator(void)
16101648
rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
16111649
rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
16121650
rb_define_method(cState, "generate", cState_generate, -1);
1651+
rb_define_alias(cState, "generate_new", "generate"); // :nodoc:
16131652

16141653
rb_define_singleton_method(cState, "generate", cState_m_generate, 3);
16151654

@@ -1680,6 +1719,7 @@ void Init_generator(void)
16801719
sym_script_safe = ID2SYM(rb_intern("script_safe"));
16811720
sym_escape_slash = ID2SYM(rb_intern("escape_slash"));
16821721
sym_strict = ID2SYM(rb_intern("strict"));
1722+
sym_as_json = ID2SYM(rb_intern("as_json"));
16831723

16841724
usascii_encindex = rb_usascii_encindex();
16851725
utf8_encindex = rb_utf8_encindex();

java/src/json/ext/Generator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,14 @@ void generate(ThreadContext context, Session session, IRubyObject object, Output
510510
RubyString generateNew(ThreadContext context, Session session, IRubyObject object) {
511511
GeneratorState state = session.getState(context);
512512
if (state.strict()) {
513+
if (state.getAsJSON() != null ) {
514+
IRubyObject value = state.getAsJSON().call(context, object);
515+
Handler handler = getHandlerFor(context.runtime, value);
516+
if (handler == GENERIC_HANDLER) {
517+
throw Utils.buildGeneratorError(context, object, value + " returned by as_json not allowed in JSON").toThrowable();
518+
}
519+
return handler.generateNew(context, session, value);
520+
}
513521
throw Utils.buildGeneratorError(context, object, object + " not allowed in JSON").toThrowable();
514522
} else if (object.respondsTo("to_json")) {
515523
IRubyObject result = object.callMethod(context, "to_json", state);

java/src/json/ext/GeneratorState.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.jruby.RubyInteger;
1515
import org.jruby.RubyNumeric;
1616
import org.jruby.RubyObject;
17+
import org.jruby.RubyProc;
1718
import org.jruby.RubyString;
1819
import org.jruby.anno.JRubyMethod;
1920
import org.jruby.runtime.Block;
@@ -22,6 +23,7 @@
2223
import org.jruby.runtime.Visibility;
2324
import org.jruby.runtime.builtin.IRubyObject;
2425
import org.jruby.util.ByteList;
26+
import org.jruby.util.TypeConverter;
2527

2628
/**
2729
* The <code>JSON::Ext::Generator::State</code> class.
@@ -58,6 +60,8 @@ public class GeneratorState extends RubyObject {
5860
*/
5961
private ByteList arrayNl = ByteList.EMPTY_BYTELIST;
6062

63+
private RubyProc asJSON;
64+
6165
/**
6266
* The maximum level of nesting of structures allowed.
6367
* <code>0</code> means disabled.
@@ -211,6 +215,7 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject vOrig) {
211215
this.spaceBefore = orig.spaceBefore;
212216
this.objectNl = orig.objectNl;
213217
this.arrayNl = orig.arrayNl;
218+
this.asJSON = orig.asJSON;
214219
this.maxNesting = orig.maxNesting;
215220
this.allowNaN = orig.allowNaN;
216221
this.asciiOnly = orig.asciiOnly;
@@ -227,7 +232,7 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject vOrig) {
227232
* the result. If no valid JSON document can be created this method raises
228233
* a GeneratorError exception.
229234
*/
230-
@JRubyMethod
235+
@JRubyMethod(alias="generate_new")
231236
public IRubyObject generate(ThreadContext context, IRubyObject obj, IRubyObject io) {
232237
IRubyObject result = Generator.generateJson(context, obj, this, io);
233238
RuntimeInfo info = RuntimeInfo.forRuntime(context.runtime);
@@ -247,7 +252,7 @@ public IRubyObject generate(ThreadContext context, IRubyObject obj, IRubyObject
247252
return resultString;
248253
}
249254

250-
@JRubyMethod
255+
@JRubyMethod(alias="generate_new")
251256
public IRubyObject generate(ThreadContext context, IRubyObject obj) {
252257
return generate(context, obj, context.nil);
253258
}
@@ -353,6 +358,22 @@ public IRubyObject array_nl_set(ThreadContext context,
353358
return arrayNl;
354359
}
355360

361+
public RubyProc getAsJSON() {
362+
return asJSON;
363+
}
364+
365+
@JRubyMethod(name="as_json")
366+
public IRubyObject as_json_get(ThreadContext context) {
367+
return asJSON == null ? context.getRuntime().getFalse() : asJSON;
368+
}
369+
370+
@JRubyMethod(name="as_json=")
371+
public IRubyObject as_json_set(ThreadContext context,
372+
IRubyObject asJSON) {
373+
this.asJSON = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc");
374+
return asJSON;
375+
}
376+
356377
@JRubyMethod(name="check_circular?")
357378
public RubyBoolean check_circular_p(ThreadContext context) {
358379
return RubyBoolean.newBoolean(context, maxNesting != 0);
@@ -487,6 +508,8 @@ public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) {
487508
ByteList arrayNl = opts.getString("array_nl");
488509
if (arrayNl != null) this.arrayNl = arrayNl;
489510

511+
this.asJSON = opts.getProc("as_json");
512+
490513
ByteList objectNl = opts.getString("object_nl");
491514
if (objectNl != null) this.objectNl = objectNl;
492515

@@ -522,6 +545,7 @@ public RubyHash to_h(ThreadContext context) {
522545
result.op_aset(context, runtime.newSymbol("space_before"), space_before_get(context));
523546
result.op_aset(context, runtime.newSymbol("object_nl"), object_nl_get(context));
524547
result.op_aset(context, runtime.newSymbol("array_nl"), array_nl_get(context));
548+
result.op_aset(context, runtime.newSymbol("as_json"), as_json_get(context));
525549
result.op_aset(context, runtime.newSymbol("allow_nan"), allow_nan_p(context));
526550
result.op_aset(context, runtime.newSymbol("ascii_only"), ascii_only_p(context));
527551
result.op_aset(context, runtime.newSymbol("max_nesting"), max_nesting_get(context));

java/src/json/ext/OptionsReader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import org.jruby.RubyClass;
1111
import org.jruby.RubyHash;
1212
import org.jruby.RubyNumeric;
13+
import org.jruby.RubyProc;
1314
import org.jruby.RubyString;
1415
import org.jruby.runtime.ThreadContext;
1516
import org.jruby.runtime.builtin.IRubyObject;
1617
import org.jruby.util.ByteList;
18+
import org.jruby.util.TypeConverter;
1719

1820
final class OptionsReader {
1921
private final ThreadContext context;
@@ -110,4 +112,10 @@ public RubyHash getHash(String key) {
110112
if (value == null || value.isNil()) return new RubyHash(runtime);
111113
return (RubyHash) value;
112114
}
115+
116+
RubyProc getProc(String key) {
117+
IRubyObject value = get(key);
118+
if (value == null) return null;
119+
return (RubyProc)TypeConverter.convertToType(value, runtime.getProc(), "to_proc");
120+
}
113121
}

0 commit comments

Comments
 (0)