Class: JSON::Ext::Parser
- Inherits:
-
Object
- Object
- JSON::Ext::Parser
- Defined in:
- ext/json/ext/parser/parser.c,
ext/json/ext/parser/parser.c
Overview
Instance Method Summary collapse
-
#new(source, opts) ⇒ Object
constructor
Creates a new JSON::Ext::Parser instance for the string source.
-
#parse ⇒ Object
Parses the current JSON text source and returns the complete data structure as a result.
-
#quirks_mode? ⇒ Boolean
Returns a true, if this parser is in quirks_mode, false otherwise.
-
#source ⇒ Object
Returns a copy of the current source string, that was used to construct this Parser.
Constructor Details
#new(source, opts) ⇒ Object
Creates a new JSON::Ext::Parser instance for the string source.
Creates a new JSON::Ext::Parser instance for the string source.
It will be configured by the opts hash. opts can have the following keys:
opts can have the following keys:
-
max_nesting: The maximum depth of nesting allowed in the parsed data structures. Disable depth checking with :max_nesting => false|nil|0, it defaults to 19.
-
allow_nan: If set to true, allow NaN, Infinity and -Infinity in defiance of RFC 4627 to be parsed by the Parser. This option defaults to false.
-
symbolize_names: If set to true, returns symbols for the names (keys) in a JSON object. Otherwise strings are returned, which is also the default.
-
create_additions: If set to false, the Parser doesn’t create additions even if a matchin class and create_id was found. This option defaults to true.
-
object_class: Defaults to Hash
-
array_class: Defaults to Array
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 |
# File 'ext/json/ext/parser/parser.c', line 1644
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE source, opts;
GET_PARSER_INIT;
if (RTEST(json->Vsource)) {
rb_raise(rb_eTypeError, "already initialized instance");
}
rb_scan_args(argc, argv, "11", &source, &opts);
if (!NIL_P(opts)) {
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
if (NIL_P(opts)) {
rb_raise(rb_eArgError, "opts needs to be like a hash");
} else {
VALUE tmp = ID2SYM(i_max_nesting);
if (option_given_p(opts, tmp)) {
VALUE max_nesting = rb_hash_aref(opts, tmp);
if (RTEST(max_nesting)) {
Check_Type(max_nesting, T_FIXNUM);
json->max_nesting = FIX2INT(max_nesting);
} else {
json->max_nesting = 0;
}
} else {
json->max_nesting = 19;
}
tmp = ID2SYM(i_allow_nan);
if (option_given_p(opts, tmp)) {
json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
} else {
json->allow_nan = 0;
}
tmp = ID2SYM(i_symbolize_names);
if (option_given_p(opts, tmp)) {
json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
} else {
json->symbolize_names = 0;
}
tmp = ID2SYM(i_quirks_mode);
if (option_given_p(opts, tmp)) {
VALUE quirks_mode = rb_hash_aref(opts, tmp);
json->quirks_mode = RTEST(quirks_mode) ? 1 : 0;
} else {
json->quirks_mode = 0;
}
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
} else {
json->create_additions = 1;
}
tmp = ID2SYM(i_create_id);
if (option_given_p(opts, tmp)) {
PARSER_SET_REFERENCE(json, create_id, rb_hash_aref(opts, tmp));
} else {
PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0));
}
tmp = ID2SYM(i_object_class);
if (option_given_p(opts, tmp)) {
PARSER_SET_REFERENCE(json, object_class, rb_hash_aref(opts, tmp));
} else {
PARSER_SET_REFERENCE(json, object_class, Qnil);
}
tmp = ID2SYM(i_array_class);
if (option_given_p(opts, tmp)) {
PARSER_SET_REFERENCE(json, array_class, rb_hash_aref(opts, tmp));
} else {
PARSER_SET_REFERENCE(json, array_class, Qnil);
}
tmp = ID2SYM(i_match_string);
if (option_given_p(opts, tmp)) {
VALUE match_string = rb_hash_aref(opts, tmp);
PARSER_SET_REFERENCE(json, match_string, RTEST(match_string) ? match_string : Qnil);
} else {
PARSER_SET_REFERENCE(json, match_string, Qnil);
}
}
} else {
json->max_nesting = 19;
json->allow_nan = 0;
json->create_additions = 1;
PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0));
json->object_class = Qnil;
json->array_class = Qnil;
}
source = rb_convert_type(source, T_STRING, "String", "to_str");
if (!json->quirks_mode) {
source = convert_encoding(StringValue(source));
}
json->current_nesting = 0;
StringValue(source);
json->len = RSTRING_LEN(source);
json->source = RSTRING_PTR(source);;
PARSER_SET_REFERENCE(json, Vsource, source);
return self;
}
|
Instance Method Details
#parse ⇒ Object
Parses the current JSON text source and returns the complete data
structure as a result.
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 |
# File 'ext/json/ext/parser/parser.c', line 2093
static VALUE cParser_parse(VALUE self)
{
GET_PARSER;
if (json->quirks_mode) {
return cParser_parse_quirks_mode(self);
} else {
return cParser_parse_strict(self);
}
}
|
#quirks_mode? ⇒ Boolean
Returns a true, if this parser is in quirks_mode, false otherwise.
2157 2158 2159 2160 2161 |
# File 'ext/json/ext/parser/parser.c', line 2157
static VALUE cParser_quirks_mode_p(VALUE self)
{
GET_PARSER;
return json->quirks_mode ? Qtrue : Qfalse;
}
|
#source ⇒ Object
Returns a copy of the current source string, that was used to construct this Parser.
2146 2147 2148 2149 2150 |
# File 'ext/json/ext/parser/parser.c', line 2146
static VALUE cParser_source(VALUE self)
{
GET_PARSER;
return rb_str_dup(json->Vsource);
}
|