Skip to content

Commit

Permalink
Parser extension (ohler55#821)
Browse files Browse the repository at this point in the history
Reorganize to support Oj::Parser extensions in C.
  • Loading branch information
ohler55 authored Nov 1, 2022
1 parent e2c0fbd commit 24347a4
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 220 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.13.22 - 2022-11-01

- Reorganized Oj::Parser code to allow for parser extensions in C.

## 3.13.21 - 2022-08-19

- Bug parsing big numbers fixed in the SAJ parser.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ Follow [@peterohler on Twitter](http://twitter.com/peterohler) for announcements

- *Agoo-C, a high performance C web server supporting GraphQL on GitHub*: https://github.com/ohler55/agoo-c

- *oj-introspect, an example of creating an Oj parser extension in C*: https://github.com/meinac/oj-introspect

#### Contributing

+ Provide a Pull Request off the `develop` branch.
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ static void hash_set_num(struct _parseInfo *pi, Val kval, NumInfo ni) {
struct timespec ts;
ts.tv_sec = ni->i;
ts.tv_nsec = nsec;
parent->val = rb_time_timespec_new(&ts, ni->exp);
parent->val = rb_time_timespec_new(&ts, (int)ni->exp);
} else {
parent->val = rb_time_nano_new(ni->i, (long)nsec);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ static int hat_num(ParseInfo pi, Val parent, Val kval, NumInfo ni) {
struct timespec ts;
ts.tv_sec = ni->i;
ts.tv_nsec = nsec;
parent->val = rb_time_timespec_new(&ts, ni->exp);
parent->val = rb_time_timespec_new(&ts, (int)ni->exp);
} else {
parent->val = rb_time_nano_new(ni->i, (long)nsec);
}
Expand Down
26 changes: 26 additions & 0 deletions ext/oj/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,32 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
}

// Create a new parser without setting the delegate. The parser is
// wrapped. The parser is (ojParser)DATA_PTR(value) where value is the return
// from this function. A delegate must be added before the parser can be
// used. Optionally oj_parser_set_options can be called if the options are not
// set directly.
VALUE oj_parser_new() {
ojParser p = ALLOC(struct _ojParser);

#if HAVE_RB_EXT_RACTOR_SAFE
// This doesn't seem to do anything.
rb_ext_ractor_safe(true);
#endif
memset(p, 0, sizeof(struct _ojParser));
buf_init(&p->key);
buf_init(&p->buf);
p->map = value_map;

return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
}

// Set set the options from a hash (ropts).
void oj_parser_set_option(ojParser p, VALUE ropts) {
Check_Type(ropts, T_HASH);
rb_hash_foreach(ropts, opt_cb, (VALUE)p);
}

/* Document-method: method_missing(value)
* call-seq: method_missing(value)
*
Expand Down
11 changes: 11 additions & 0 deletions ext/oj/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,15 @@ typedef struct _ojParser {
bool just_one;
} * ojParser;

// Create a new parser without setting the delegate. The parser is
// wrapped. The parser is (ojParser)DATA_PTR(value) where value is the return
// from this function. A delegate must be added before the parser can be
// used. Optionally oj_parser_set_options can be called if the options are not
// set directly.
extern VALUE oj_parser_new();

// Set set the options from a hash (ropts).
extern void oj_parser_set_option(ojParser p, VALUE ropts);


#endif /* OJ_PARSER_H */
Loading

0 comments on commit 24347a4

Please sign in to comment.