Class: JSON::Ext::Generator::State
- Defined in:
- ext/json/ext/generator/generator.c
Class Method Summary collapse
-
.from_state(opts) ⇒ Object
Creates a State object from opts, which ought to be Hash to create a new State instance configured by opts, something else to create an unconfigured instance.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return the value returned by method
name
. -
#allow_nan? ⇒ Boolean
Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.
-
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#array_nl=(array_nl) ⇒ Object
This string is put at the end of a line that holds a JSON array.
-
#ascii_only? ⇒ Boolean
Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.
-
#check_circular? ⇒ Boolean
Returns true, if circular data structures should be checked, otherwise returns false.
-
#configure(opts) ⇒ Object
Configure this State instance with the Hash opts, and return itself.
-
#depth ⇒ Object
This integer returns the current depth of data structure nesting.
-
#depth=(depth) ⇒ Object
This sets the maximum level of data structure nesting in the generated JSON to the integer depth, max_nesting = 0 if no maximum should be checked.
-
#generate(obj) ⇒ Object
Generates a valid JSON document from object
obj
and returns the result. -
#indent ⇒ Object
This string is used to indent levels in the JSON text.
-
#indent=(indent) ⇒ Object
This string is used to indent levels in the JSON text.
-
#new(opts = {}) ⇒ Object
constructor
Instantiates a new State object, configured by opts.
-
#initialize_copy(orig) ⇒ Object
Initializes this object from orig if it to be duplicated/cloned and returns it.
-
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
-
#max_nesting=(depth) ⇒ Object
This sets the maximum level of data structure nesting in the generated JSON to the integer depth, max_nesting = 0 if no maximum should be checked.
-
#object_nl ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
-
#object_nl=(object_nl) ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
-
#space ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
-
#space=(space) ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
-
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
-
#space_before=(space_before) ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
-
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
Constructor Details
#new(opts = {}) ⇒ Object
Instantiates a new State object, configured by opts.
opts can have the following keys:
-
indent: a string used to indent levels (default: ”),
-
space: a string that is put after, a : or , delimiter (default: ”),
-
space_before: a string that is put before a : pair delimiter (default: ”),
-
object_nl: a string that is put at the end of a JSON object (default: ”),
-
array_nl: a string that is put at the end of a JSON array (default: ”),
-
allow_nan: true if NaN, Infinity, and -Infinity should be generated, otherwise an exception is thrown, if these values are encountered. This options defaults to false.
993 994 995 996 997 998 999 1000 1001 |
# File 'ext/json/ext/generator/generator.c', line 993
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE opts;
GET_STATE(self);
state->max_nesting = 19;
rb_scan_args(argc, argv, "01", &opts);
if (!NIL_P(opts)) cState_configure(self, opts);
return self;
}
|
Class Method Details
.from_state(opts) ⇒ Object
Creates a State object from opts, which ought to be Hash to create a new State instance configured by opts, something else to create an unconfigured instance. If opts is a State object, it is just returned.
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 |
# File 'ext/json/ext/generator/generator.c', line 1036
static VALUE cState_from_state_s(VALUE self, VALUE opts)
{
if (rb_obj_is_kind_of(opts, self)) {
return opts;
} else if (rb_obj_is_kind_of(opts, rb_cHash)) {
return rb_funcall(self, i_new, 1, opts);
} else {
if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
}
return rb_funcall(CJSON_SAFE_STATE_PROTOTYPE, i_dup, 0);
}
}
|
Instance Method Details
#[](name) ⇒ Object
Return the value returned by method name
.
725 726 727 728 729 730 731 732 733 |
# File 'ext/json/ext/generator/generator.c', line 725
static VALUE cState_aref(VALUE self, VALUE name)
{
GET_STATE(self);
if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
return rb_funcall(self, i_send, 1, name);
} else {
return Qnil;
}
}
|
#allow_nan? ⇒ Boolean
Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.
1276 1277 1278 1279 1280 |
# File 'ext/json/ext/generator/generator.c', line 1276
static VALUE cState_allow_nan_p(VALUE self)
{
GET_STATE(self);
return state->allow_nan ? Qtrue : Qfalse;
}
|
#array_nl ⇒ Object
This string is put at the end of a line that holds a JSON array.
1202 1203 1204 1205 1206 |
# File 'ext/json/ext/generator/generator.c', line 1202
static VALUE cState_array_nl(VALUE self)
{
GET_STATE(self);
return state->array_nl ? rb_str_new2(state->array_nl) : rb_str_new2("");
}
|
#array_nl=(array_nl) ⇒ Object
This string is put at the end of a line that holds a JSON array.
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 |
# File 'ext/json/ext/generator/generator.c', line 1213
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
{
int len;
GET_STATE(self);
Check_Type(array_nl, T_STRING);
len = RSTRING_LEN(array_nl);
if (len == 0) {
if (state->array_nl) {
ruby_xfree(state->array_nl);
state->array_nl = NULL;
}
} else {
if (state->array_nl) ruby_xfree(state->array_nl);
state->array_nl = strdup(RSTRING_PTR(array_nl));
state->array_nl_len = len;
}
return Qnil;
}
|
#ascii_only? ⇒ Boolean
Returns true, if NaN, Infinity, and -Infinity should be generated, otherwise returns false.
1288 1289 1290 1291 1292 |
# File 'ext/json/ext/generator/generator.c', line 1288
static VALUE cState_ascii_only_p(VALUE self)
{
GET_STATE(self);
return state->ascii_only ? Qtrue : Qfalse;
}
|
#check_circular? ⇒ Boolean
Returns true, if circular data structures should be checked, otherwise returns false.
1239 1240 1241 1242 1243 |
# File 'ext/json/ext/generator/generator.c', line 1239
static VALUE cState_check_circular_p(VALUE self)
{
GET_STATE(self);
return state->max_nesting ? Qtrue : Qfalse;
}
|
#configure(opts) ⇒ Object
Configure this State instance with the Hash opts, and return itself.
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'ext/json/ext/generator/generator.c', line 619
static VALUE cState_configure(VALUE self, VALUE opts)
{
VALUE tmp;
GET_STATE(self);
tmp = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
if (NIL_P(tmp)) tmp = rb_convert_type(opts, T_HASH, "Hash", "to_h");
if (NIL_P(tmp)) {
rb_raise(rb_eArgError, "opts has to be hash like or convertable into a hash");
}
opts = tmp;
tmp = rb_hash_aref(opts, ID2SYM(i_indent));
if (RTEST(tmp)) {
int len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
state->indent = fstrndup(RSTRING_PTR(tmp), len);
state->indent_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_space));
if (RTEST(tmp)) {
int len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
state->space = fstrndup(RSTRING_PTR(tmp), len);
state->space_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
if (RTEST(tmp)) {
int len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
state->space_before = fstrndup(RSTRING_PTR(tmp), len);
state->space_before_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
if (RTEST(tmp)) {
int len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
state->array_nl = fstrndup(RSTRING_PTR(tmp), len);
state->array_nl_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
if (RTEST(tmp)) {
int len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
state->object_nl = fstrndup(RSTRING_PTR(tmp), len);
state->object_nl_len = len;
}
tmp = ID2SYM(i_max_nesting);
state->max_nesting = 19;
if (option_given_p(opts, tmp)) {
VALUE max_nesting = rb_hash_aref(opts, tmp);
if (RTEST(max_nesting)) {
Check_Type(max_nesting, T_FIXNUM);
state->max_nesting = FIX2LONG(max_nesting);
} else {
state->max_nesting = 0;
}
}
tmp = ID2SYM(i_depth);
state->depth = 0;
if (option_given_p(opts, tmp)) {
VALUE depth = rb_hash_aref(opts, tmp);
if (RTEST(depth)) {
Check_Type(depth, T_FIXNUM);
state->depth = FIX2LONG(depth);
} else {
state->depth = 0;
}
}
tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
state->allow_nan = RTEST(tmp);
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
state->ascii_only = RTEST(tmp);
return self;
}
|
#depth ⇒ Object
This integer returns the current depth of data structure nesting.
1299 1300 1301 1302 1303 |
# File 'ext/json/ext/generator/generator.c', line 1299
static VALUE cState_depth(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->depth);
}
|
#depth=(depth) ⇒ Object
This sets the maximum level of data structure nesting in the generated JSON to the integer depth, max_nesting = 0 if no maximum should be checked.
1311 1312 1313 1314 1315 1316 |
# File 'ext/json/ext/generator/generator.c', line 1311
static VALUE cState_depth_set(VALUE self, VALUE depth)
{
GET_STATE(self);
Check_Type(depth, T_FIXNUM);
return state->depth = FIX2LONG(depth);
}
|
#generate(obj) ⇒ Object
Generates a valid JSON document from object obj
and returns the result. If no valid JSON document can be created this method raises a GeneratorError exception.
964 965 966 967 968 969 970 971 972 973 974 975 |
# File 'ext/json/ext/generator/generator.c', line 964
static VALUE cState_generate(VALUE self, VALUE obj)
{
VALUE result = cState_partial_generate(self, obj);
VALUE re, args[2];
args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
args[1] = CRegexp_MULTILINE;
re = rb_class_new_instance(2, args, rb_cRegexp);
if (NIL_P(rb_funcall(re, i_match, 1, result))) {
rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
}
return result;
}
|
#indent ⇒ Object
This string is used to indent levels in the JSON text.
1055 1056 1057 1058 1059 |
# File 'ext/json/ext/generator/generator.c', line 1055
static VALUE cState_indent(VALUE self)
{
GET_STATE(self);
return state->indent ? rb_str_new2(state->indent) : rb_str_new2("");
}
|
#indent=(indent) ⇒ Object
This string is used to indent levels in the JSON text.
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 |
# File 'ext/json/ext/generator/generator.c', line 1066
static VALUE cState_indent_set(VALUE self, VALUE indent)
{
int len;
GET_STATE(self);
Check_Type(indent, T_STRING);
len = RSTRING_LEN(indent);
if (len == 0) {
if (state->indent) {
ruby_xfree(state->indent);
state->indent = NULL;
state->indent_len = 0;
}
} else {
if (state->indent) ruby_xfree(state->indent);
state->indent = strdup(RSTRING_PTR(indent));
state->indent_len = len;
}
return Qnil;
}
|
#initialize_copy(orig) ⇒ Object
Initializes this object from orig if it to be duplicated/cloned and returns it.
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
# File 'ext/json/ext/generator/generator.c', line 1009
static VALUE cState_init_copy(VALUE obj, VALUE orig)
{
JSON_Generator_State *objState, *origState;
Data_Get_Struct(obj, JSON_Generator_State, objState);
Data_Get_Struct(orig, JSON_Generator_State, origState);
if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
MEMCPY(objState, origState, JSON_Generator_State, 1);
objState->indent = fstrndup(origState->indent, origState->indent_len);
objState->space = fstrndup(origState->space, origState->space_len);
objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
if (origState->array_delim) objState->array_delim = fbuffer_dup(origState->array_delim);
if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
return obj;
}
|
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
1251 1252 1253 1254 1255 |
# File 'ext/json/ext/generator/generator.c', line 1251
static VALUE cState_max_nesting(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->max_nesting);
}
|
#max_nesting=(depth) ⇒ Object
This sets the maximum level of data structure nesting in the generated JSON to the integer depth, max_nesting = 0 if no maximum should be checked.
1263 1264 1265 1266 1267 1268 |
# File 'ext/json/ext/generator/generator.c', line 1263
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
GET_STATE(self);
Check_Type(depth, T_FIXNUM);
return state->max_nesting = FIX2LONG(depth);
}
|
#object_nl ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
1166 1167 1168 1169 1170 |
# File 'ext/json/ext/generator/generator.c', line 1166
static VALUE cState_object_nl(VALUE self)
{
GET_STATE(self);
return state->object_nl ? rb_str_new2(state->object_nl) : rb_str_new2("");
}
|
#object_nl=(object_nl) ⇒ Object
This string is put at the end of a line that holds a JSON object (or Hash).
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 |
# File 'ext/json/ext/generator/generator.c', line 1178
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
{
int len;
GET_STATE(self);
Check_Type(object_nl, T_STRING);
len = RSTRING_LEN(object_nl);
if (len == 0) {
if (state->object_nl) {
ruby_xfree(state->object_nl);
state->object_nl = NULL;
}
} else {
if (state->object_nl) ruby_xfree(state->object_nl);
state->object_nl = strdup(RSTRING_PTR(object_nl));
state->object_nl_len = len;
}
return Qnil;
}
|
#space ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
1092 1093 1094 1095 1096 |
# File 'ext/json/ext/generator/generator.c', line 1092
static VALUE cState_space(VALUE self)
{
GET_STATE(self);
return state->space ? rb_str_new2(state->space) : rb_str_new2("");
}
|
#space=(space) ⇒ Object
This string is used to insert a space between the tokens in a JSON string.
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 |
# File 'ext/json/ext/generator/generator.c', line 1104
static VALUE cState_space_set(VALUE self, VALUE space)
{
int len;
GET_STATE(self);
Check_Type(space, T_STRING);
len = RSTRING_LEN(space);
if (len == 0) {
if (state->space) {
ruby_xfree(state->space);
state->space = NULL;
state->space_len = 0;
}
} else {
if (state->space) ruby_xfree(state->space);
state->space = strdup(RSTRING_PTR(space));
state->space_len = len;
}
return Qnil;
}
|
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
1129 1130 1131 1132 1133 |
# File 'ext/json/ext/generator/generator.c', line 1129
static VALUE cState_space_before(VALUE self)
{
GET_STATE(self);
return state->space_before ? rb_str_new2(state->space_before) : rb_str_new2("");
}
|
#space_before=(space_before) ⇒ Object
This string is used to insert a space before the ‘:’ in JSON objects.
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 |
# File 'ext/json/ext/generator/generator.c', line 1140
static VALUE cState_space_before_set(VALUE self, VALUE space_before)
{
int len;
GET_STATE(self);
Check_Type(space_before, T_STRING);
len = RSTRING_LEN(space_before);
if (len == 0) {
if (state->space_before) {
ruby_xfree(state->space_before);
state->space_before = NULL;
state->space_before_len = 0;
}
} else {
if (state->space_before) ruby_xfree(state->space_before);
state->space_before = strdup(RSTRING_PTR(space_before));
state->space_before_len = len;
}
return Qnil;
}
|
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'ext/json/ext/generator/generator.c', line 704
static VALUE cState_to_h(VALUE self)
{
VALUE result = rb_hash_new();
GET_STATE(self);
rb_hash_aset(result, ID2SYM(i_indent), rb_str_new(state->indent, state->indent_len));
rb_hash_aset(result, ID2SYM(i_space), rb_str_new(state->space, state->space_len));
rb_hash_aset(result, ID2SYM(i_space_before), rb_str_new(state->space_before, state->space_before_len));
rb_hash_aset(result, ID2SYM(i_object_nl), rb_str_new(state->object_nl, state->object_nl_len));
rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
return result;
}
|