Implement JSON built-in object

JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg@inf.u-szeged.hu
This commit is contained in:
Zoltan Herczeg
2015-07-07 00:57:21 -07:00
parent cadc8f40d4
commit d1a5f7fc87
10 changed files with 2866 additions and 5 deletions
@@ -202,12 +202,14 @@ OBJECT_VALUE (LIT_MAGIC_STRING_MATH_UL,
ECMA_PROPERTY_CONFIGURABLE)
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_MATH_BUILTIN */
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN
// ECMA-262 v5, 15.1.5.2
CP_UNIMPLEMENTED_VALUE (LIT_MAGIC_STRING_JSON_U,
ecma_builtin_get (ECMA_BUILTIN_ID_JSON),
ECMA_PROPERTY_WRITABLE,
ECMA_PROPERTY_NOT_ENUMERABLE,
ECMA_PROPERTY_CONFIGURABLE)
OBJECT_VALUE (LIT_MAGIC_STRING_JSON_U,
ecma_builtin_get (ECMA_BUILTIN_ID_JSON),
ECMA_PROPERTY_WRITABLE,
ECMA_PROPERTY_NOT_ENUMERABLE,
ECMA_PROPERTY_CONFIGURABLE)
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN */
#ifdef CONFIG_ECMA_COMPACT_PROFILE
OBJECT_VALUE (LIT_MAGIC_STRING_COMPACT_PROFILE_ERROR_UL,
@@ -0,0 +1,399 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmahelpers Helpers for operations with ECMA data types
* @{
*/
#include "ecma-alloc.h"
#include "ecma-helpers.h"
#include "ecma-builtin-helpers.h"
#define LIST_BLOCK_SIZE 256UL
/**
* Allocate memory for elements.
*
* @return pointer to current position of the allocated memory.
*/
void **
realloc_list (list_ctx_t *ctx_p) /**< list context */
{
size_t old_block_size = static_cast<size_t> (ctx_p->block_end_p - ctx_p->block_start_p);
size_t new_block_size = old_block_size + LIST_BLOCK_SIZE;
size_t current_ptr_offset = static_cast<size_t> (ctx_p->current_p - ctx_p->block_start_p);
void **new_block_start_p = (void **) mem_heap_alloc_block (new_block_size, MEM_HEAP_ALLOC_SHORT_TERM);
if (ctx_p->current_p)
{
memcpy (new_block_start_p, ctx_p->block_start_p, static_cast<size_t> (current_ptr_offset));
mem_heap_free_block (ctx_p->block_start_p);
}
ctx_p->block_start_p = new_block_start_p;
ctx_p->block_end_p = new_block_start_p + new_block_size;
ctx_p->current_p = new_block_start_p + current_ptr_offset;
return ctx_p->current_p;
} /* realloc_list */
/**
* Append element to the list.
*/
void
list_append (list_ctx_t *ctx_p, /**< list context */
void *element_p) /**< element that should be stored */
{
void **current_p = ctx_p->current_p;
if (current_p + 1 > ctx_p->block_end_p)
{
current_p = realloc_list (ctx_p);
}
*current_p = element_p;
ctx_p->current_p++;
} /* list_append */
/**
* Check the element existance in the list.
*
* @return true if the list already has the value.
*/
bool
list_has_element (list_ctx_t *ctx_p, /**< list context */
void *element_p) /**< element that should be checked */
{
for (void **current_p = ctx_p->block_start_p;
current_p < ctx_p->current_p;
current_p++)
{
if (*current_p == element_p)
{
return true;
}
}
return false;
} /* list_has_element */
/**
* Check the element existance in the list for ecma strings.
*
* @return true if the list already has the value.
*/
bool
list_has_ecma_string_element (list_ctx_t *ctx_p, /**< list context */
ecma_string_t *string_p) /**< element that should be checked */
{
for (void **current_p = ctx_p->block_start_p;
current_p < ctx_p->current_p;
current_p++)
{
if (ecma_compare_ecma_strings ((ecma_string_t *) *current_p, string_p))
{
return true;
}
}
return false;
} /* list_has_element */
/**
* Check the state of the list allocation.
*
* @return true if the list is already allocated.
*/
static bool
list_is_allocated (list_ctx_t *ctx_p) /**< list context */
{
return ctx_p->block_start_p < ctx_p->block_end_p;
} /* list_is_allocated */
/**
* Check if the list is empty.
*
* @return true if the list is empty.
*/
bool
list_is_empty (list_ctx_t *ctx_p)
{
return ctx_p->block_start_p == ctx_p->current_p;
} /* list_is_empty */
/**
* Set the current memory position to the previous element.
*/
void
list_remove_last_element (list_ctx_t *ctx_p) /**< list context */
{
if (list_is_empty (ctx_p))
{
return;
}
ctx_p->current_p = NULL;
ctx_p->current_p--;
} /* list_remove_last_element */
/**
* Free the allocated list.
*/
void
free_list (list_ctx_t *ctx_p) /**< list context */
{
if (!list_is_allocated (ctx_p))
{
return;
}
mem_heap_free_block (ctx_p->block_start_p);
ctx_p->block_start_p = NULL;
ctx_p->block_end_p = NULL;
ctx_p->current_p = NULL;
} /* free_list */
/**
* Free the stored ecma string elements and the list.
*/
void
free_list_with_ecma_string_content (list_ctx_t *ctx_p) /**< list context */
{
if (!list_is_allocated (ctx_p))
{
return;
}
for (void **current_p = ctx_p->block_start_p;
current_p < ctx_p->current_p;
current_p++)
{
ecma_deref_ecma_string ((ecma_string_t *) *current_p);
}
free_list (ctx_p);
} /* free_list_with_ecma_string_content */
/**
* Common function to concatenate key-value pairs into an ecma-string.
*
* See also:
* ECMA-262 v5, 15.12.3
*
* Used by:
* - ecma_builtin_helper_json_create_formatted_json step 10.b.ii
* - ecma_builtin_helper_json_create_non_formatted_json step 10.a.i
*
* @return pointer to ecma-string
* Returned value must be freed with ecma_deref_ecma_string.
*/
ecma_string_t *
ecma_builtin_helper_json_create_separated_properties (list_ctx_t *partial_p, /**< list of key-value pairs*/
ecma_string_t *separator_p) /**< separator*/
{
ecma_string_t *properties_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
ecma_string_t *tmp_str_p;
uint32_t index = 0;
for (void **current_p = partial_p->block_start_p;
current_p < partial_p->current_p;
current_p++, index++)
{
if (index == 0)
{
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, (ecma_string_t *) *current_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
continue;
}
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, separator_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, (ecma_string_t *) *current_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
}
return properties_str_p;
} /* ecma_builtin_json_helper_create_separated_properties */
/**
* Common function to create a formatted JSON string.
*
* See also:
* ECMA-262 v5, 15.12.3
*
* Used by:
* - ecma_builtin_json_object step 10.b
* - ecma_builtin_json_array step 10.b
*
* @return completion value
* Returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
ecma_builtin_helper_json_create_formatted_json (ecma_string_t *left_bracket_p, /**< left bracket*/
ecma_string_t *right_bracket_p, /**< right bracket*/
ecma_string_t *stepback_p, /**< stepback*/
list_ctx_t *partial_p, /**< list of key-value pairs*/
stringify_context_t *context_p) /**< context*/
{
/* 10.b */
ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
ecma_string_t *line_feed_p = ecma_get_magic_string (LIT_MAGIC_STRING_NEW_LINE_CHAR);
ecma_string_t *properties_str_p;
ecma_string_t *separator_p;
/* 10.b.i */
ecma_string_t *tmp_str_p = ecma_concat_ecma_strings (comma_str_p, line_feed_p);
ecma_deref_ecma_string (comma_str_p);
separator_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (separator_p, context_p->indent_str_p);
ecma_deref_ecma_string (separator_p);
separator_p = tmp_str_p;
/* 10.b.ii */
properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, separator_p);
ecma_deref_ecma_string (separator_p);
/* 10.b.iii */
ecma_string_t *final_str_p;
tmp_str_p = ecma_concat_ecma_strings (left_bracket_p, line_feed_p);
final_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (final_str_p, context_p->indent_str_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (final_str_p, properties_str_p);
ecma_deref_ecma_string (final_str_p);
ecma_deref_ecma_string (properties_str_p);
final_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (final_str_p, line_feed_p);
ecma_deref_ecma_string (line_feed_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (final_str_p, stepback_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (final_str_p, right_bracket_p);
ecma_deref_ecma_string (final_str_p);
final_str_p = tmp_str_p;
return ecma_make_normal_completion_value (ecma_make_string_value (final_str_p));
} /* ecma_builtin_json_helper_create_formatted_json */
/**
* Common function to create a non-formatted JSON string.
*
* See also:
* ECMA-262 v5, 15.12.3
*
* Used by:
* - ecma_builtin_json_object step 10.a
* - ecma_builtin_json_array step 10.a
*
* @return completion value
* Returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
ecma_builtin_helper_json_create_non_formatted_json (ecma_string_t *left_bracket_p, /**< left bracket*/
ecma_string_t *right_bracket_p, /**< right bracket*/
list_ctx_t *partial_p) /**< list of key-value pairs*/
{
/* 10.a */
ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
ecma_string_t *properties_str_p;
ecma_string_t *tmp_str_p;
/* 10.a.i */
properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, comma_str_p);
ecma_deref_ecma_string (comma_str_p);
/* 10.a.ii */
tmp_str_p = ecma_concat_ecma_strings (left_bracket_p, properties_str_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
tmp_str_p = ecma_concat_ecma_strings (properties_str_p, right_bracket_p);
ecma_deref_ecma_string (properties_str_p);
properties_str_p = tmp_str_p;
return ecma_make_normal_completion_value (ecma_make_string_value (properties_str_p));
} /* ecma_builtin_json_helper_create_non_formatted_json */
/**
* Convert decimal value to 4 digit hexadecimal string value.
*
* See also:
* ECMA-262 v5, 15.12.3
*
* Used by:
* - ecma_builtin_json_quote step 2.c.iii
*
* @return pointer to ecma-string
* Returned value must be freed with ecma_deref_ecma_string.
*/
ecma_string_t *
ecma_builtin_helper_json_create_hex_digit_ecma_string (uint8_t value) /**< value in decimal*/
{
/* 2.c.iii */
ecma_string_t *hex_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
MEM_DEFINE_LOCAL_ARRAY (hex_buff, 4, lit_utf8_byte_t);
for (uint32_t i = 0; i < 4; i++)
{
uint8_t remainder = value % 16;
lit_utf8_byte_t ch = ' ';
if (remainder < 10)
{
ch = (lit_utf8_byte_t) (LIT_CHAR_0 + remainder);
}
else
{
uint8_t a = (uint8_t) (remainder - 10);
ch = (lit_utf8_byte_t) (LIT_CHAR_LOWERCASE_A + a);
}
hex_buff[3 - i] = ch;
value = value / 16;
}
ecma_deref_ecma_string (hex_str_p);
hex_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) hex_buff, 4);
MEM_FINALIZE_LOCAL_ARRAY (hex_buff);
JERRY_ASSERT (ecma_string_get_length (hex_str_p));
return hex_str_p;
} /* ecma_builtin_helper_json_create_hex_digit_ecma_string */
@@ -64,6 +64,45 @@ extern ecma_number_t ecma_date_make_date (ecma_number_t day, ecma_number_t time)
extern ecma_number_t ecma_date_time_clip (ecma_number_t time);
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN */
typedef struct
{
void **block_start_p;
void **block_end_p;
void **current_p;
} list_ctx_t;
typedef struct
{
list_ctx_t property_list;
list_ctx_t occurence_stack;
ecma_string_t *indent_str_p;
ecma_string_t *gap_str_p;
ecma_object_t *replacer_function_p;
} stringify_context_t;
/* ecma-builtin-helper-json.cpp */
extern void **realloc_list (list_ctx_t *ctx);
extern void list_append (list_ctx_t *ctx, void *element);
extern void free_list (list_ctx_t *ctx);
extern bool list_has_element (list_ctx_t *ctx, void *element);
extern bool list_has_ecma_string_element (list_ctx_t *ctx, ecma_string_t *string_p);
extern bool list_is_empty (list_ctx_t *ctx);
extern void list_remove_last_element (list_ctx_t *ctx);
extern void free_list_with_ecma_string_content (list_ctx_t *ctx);
extern ecma_string_t *ecma_builtin_helper_json_create_separated_properties (list_ctx_t *partial_p,
ecma_string_t *separator_p);
extern ecma_completion_value_t ecma_builtin_helper_json_create_formatted_json (ecma_string_t *left_bracket_p,
ecma_string_t *right_bracket_p,
ecma_string_t *stepback_p,
list_ctx_t *partial_p,
stringify_context_t *context_p);
extern ecma_completion_value_t ecma_builtin_helper_json_create_non_formatted_json (ecma_string_t *left_bracket_p,
ecma_string_t *right_bracket_p,
list_ctx_t *partial_p);
extern ecma_string_t *ecma_builtin_helper_json_create_hex_digit_ecma_string (uint8_t value);
/**
* @}
* @}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,55 @@
/* Copyright 2015 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* JSON built-in description
*/
#ifndef OBJECT_ID
# define OBJECT_ID(builtin_object_id)
#endif /* !OBJECT_ID */
#ifndef SIMPLE_VALUE
# define SIMPLE_VALUE(name, simple_value, prop_writable, prop_enumerable, prop_configurable)
#endif /* !SIMPLE_VALUE */
#ifndef NUMBER_VALUE
# define NUMBER_VALUE(name, number_value, prop_writable, prop_enumerable, prop_configurable)
#endif /* !NUMBER_VALUE */
#ifndef OBJECT_VALUE
# define OBJECT_VALUE(name, obj_getter, prop_writable, prop_enumerable, prop_configurable)
#endif /* !OBJECT_VALUE */
#ifndef ROUTINE
# define ROUTINE(name, c_function_name, args_number, length_prop_value)
#endif /* !ROUTINE */
/* Object identifier */
OBJECT_ID (ECMA_BUILTIN_ID_JSON)
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_PARSE, ecma_builtin_json_parse, 2, 2)
ROUTINE (LIT_MAGIC_STRING_STRINGIFY, ecma_builtin_json_stringify, 3, 3)
#undef OBJECT_ID
#undef SIMPLE_VALUE
#undef NUMBER_VALUE
#undef STRING_VALUE
#undef OBJECT_VALUE
#undef CP_UNIMPLEMENTED_VALUE
#undef ROUTINE
@@ -130,6 +130,16 @@ BUILTIN (ECMA_BUILTIN_ID_MATH,
math)
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_MATH_BUILTIN */
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN
/* The JSON object (15.12) */
BUILTIN (ECMA_BUILTIN_ID_JSON,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_OBJECT_PROTOTYPE,
true,
true,
json)
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN */
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
/* The Date.prototype object (15.9.4) */
BUILTIN (ECMA_BUILTIN_ID_DATE_PROTOTYPE,
@@ -683,6 +683,12 @@ ecma_object_get_class_name (ecma_object_t *obj_p) /**< object */
return LIT_MAGIC_STRING_MATH_UL;
}
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_MATH_BUILTIN */
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN
case ECMA_BUILTIN_ID_JSON:
{
return LIT_MAGIC_STRING_JSON_U;
}
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_JSON_BUILTIN */
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ERROR_BUILTINS
case ECMA_BUILTIN_ID_ERROR_PROTOTYPE:
case ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE:
+6
View File
@@ -62,6 +62,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TYPE_ERROR_UL, "TypeError")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_URI_ERROR_UL, "URIError")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MATH_UL, "Math")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_JSON_U, "JSON")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_STRINGIFY, "stringify")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PARSE, "parse")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PARSE_INT, "parseInt")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PARSE_FLOAT, "parseFloat")
@@ -217,12 +218,17 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_G_CHAR, "g")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_I_CHAR, "i")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_M_CHAR, "m")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SLASH_CHAR, "/")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BACKSLASH_CHAR, "\\")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP, "(?:)")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_LEFT_SQUARE_CHAR, "[")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR, "]")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_LEFT_BRACE_CHAR, "{")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RIGHT_BRACE_CHAR, "}")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MINUS_CHAR, "-")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COLON_CHAR, ":")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COMMA_CHAR, ",")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DOUBLE_QUOTE_CHAR, "\"")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_NEW_LINE_CHAR, "\n")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPACE_CHAR, " ")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING__EMPTY, "")
+359
View File
@@ -0,0 +1,359 @@
// Copyright 2015 University of Szeged
// Copyright 2015 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Checking primitve types
var str;
var result;
var log;
function check_parse_error (str)
{
try {
JSON.parse (str);
// Should throw a parse error.
assert (false);
} catch (e) {
}
}
str = ' null ';
assert (JSON.parse (str) === null);
str = 'true';
assert (JSON.parse (str) === true);
str = 'false';
assert (JSON.parse (str) === false);
str = '-32.5e002';
assert (JSON.parse (str) == -3250);
str = '"str"';
assert (JSON.parse (str) == "str");
check_parse_error ('undefined');
check_parse_error ('falses');
check_parse_error ('+5');
check_parse_error ('5.');
check_parse_error ('01');
check_parse_error ('0x1');
check_parse_error ('0e-');
check_parse_error ('3e+a');
check_parse_error ('55e4,');
check_parse_error ('5 true');
check_parse_error ("'str'");
// Checking objects
str = ' { "x": 0, "yy": null, "zzz": { "A": 4.0, "BB": { "1": 63e-1 }, "CCC" : false } } ';
result = JSON.parse (str);
assert (typeof result == "object");
assert (result.x === 0);
assert (result.yy === null);
assert (typeof result.zzz == "object");
assert (result.zzz.A === 4);
assert (typeof result.zzz.BB == "object");
assert (result.zzz.BB["1"] === 6.3);
assert (result.zzz.CCC === false);
check_parse_error ('{');
check_parse_error ('{{{}');
check_parse_error ('{x:5}');
check_parse_error ('{true:4}');
check_parse_error ('{"x":5 "y":6}');
check_parse_error ('{"x":5,"y":6,}');
check_parse_error ('{"x":5,,"y":6}');
// Checking arrays
str = '[{"x":[]},[[]],{}, [ null ] ]';
result = JSON.parse (str);
assert (result.length === 4);
assert (typeof result === "object");
assert (typeof result[0] === "object");
assert (typeof result[0].x === "object");
assert (result[0].x.length === 0);
assert (result[1].length === 1);
assert (result[1][0].length === 0);
assert (typeof result[2] === "object");
assert (result[3].length === 1);
assert (result[3][0] === null);
check_parse_error ('[');
check_parse_error ('[[[]');
check_parse_error ('[ true null ]');
check_parse_error ('[1,,2]');
check_parse_error ('[1,2,]');
check_parse_error ('[1] [2]');
// Checking parse with different primitive types
assert (JSON.parse (null) == null);
assert (JSON.parse (true) == true);
assert (JSON.parse (3) == 3);
try {
JSON.parse (undefined);
// Should not be reached.
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}
// Checking parse with different object types
object = { toString: function() { return false; } };
assert (JSON.parse (object) == false);
object = {"a": 3, "b": "foo"};
try {
JSON.parse (object);
// Should not be reached.
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}
array = [3, "foo"];
try {
JSON.parse (array);
// Should not be reached.
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}
assert (JSON.parse (new Number (3)) == 3);
assert (JSON.parse (new Boolean (true)) == true);
object = new String ('{"a": 3, "b": "foo"}');
result = JSON.parse (object);
assert (result.a == 3);
assert (result.b == "foo");
// Checking reviver
function toStringReviver(k, v)
{
log += "<" + k + ">:" + (typeof v == "number" ? v : "(obj)") + ", ";
return v;
}
str = '{ "a":1, "b":2, "c": { "d":4, "e": { "f":6 } } }';
log = "";
JSON.parse (str, toStringReviver);
assert (log === "<a>:1, <b>:2, <d>:4, <f>:6, <e>:(obj), <c>:(obj), <>:(obj), ");
str = '[ 32, 47, 33 ]';
log = "";
JSON.parse (str, toStringReviver);
assert (log === "<0>:32, <1>:47, <2>:33, <>:(obj), ");
// Defining properties multiple times
str = ' { "a":1, "b":2, "a":3 } ';
log = "";
JSON.parse (str, toStringReviver);
assert (log === "<a>:3, <b>:2, <>:(obj), ");
str = ' { "a":1, "b":2, "b":3 } ';
log = "";
JSON.parse (str, toStringReviver);
assert (log === "<a>:1, <b>:3, <>:(obj), ");
str = ' { "a":1, "b":{}, "b":[], "a":2, "b":3, "c":4 } ';
log = "";
JSON.parse (str, toStringReviver);
assert (log === "<a>:2, <b>:3, <c>:4, <>:(obj), ");
// Changing property value
str = ' { "a":1, "b":2, "c":3 } ';
result = JSON.parse (str, function (k, v) {
if (k == "a")
{
return 8;
}
if (k == "b")
{
return 9;
}
if (k == "c")
{
return void 0;
}
return v;
});
assert (result.a === 8);
assert (result.b === 9);
assert (result.c === void 0);
// Adding / deleting properties
str = ' { "a":1, "b":2 } ';
log = "";
result = JSON.parse (str, function (k, v) {
if (k == "a")
{
// Deleted properties must still be enumerated.
delete this["b"];
// New properties must not be enumerated.
this.c = 4;
}
if (k != "")
{
log += "<" + k + ">:" + v + " ";
}
return v;
});
assert (log === "<a>:1 <b>:undefined ");
assert (result.a === 1);
assert (result.b === void 0);
assert (result.c === 4);
// Changing properties to accessors
str = ' { "a":1, "b":2, "c":3 } ';
log = "";
JSON.parse (str, function (k, v) {
if (k == "a")
{
Object.defineProperty(this, "b", {
enumerable: true,
configurable: true,
get: function() { return 12; }
});
Object.defineProperty(this, "c", {
enumerable: true,
configurable: true,
set: function(val) { }
});
}
if (k != "")
{
log += "<" + k + ">:" + v + " ";
}
return v;
});
assert (log === "<a>:1 <b>:12 <c>:undefined ");
// Forcing extra walk steps
str = ' { "a":1, "b":2 } ';
log = "";
JSON.parse (str, function (k, v) {
if (k == "a")
{
this.b = { x:3, y:4 };
}
if (k != "")
{
log += "<" + k + ">:" + v + " ";
}
return v;
});
assert (log === "<a>:1 <x>:3 <y>:4 <b>:[object Object] ");
// Setting a property to read-only, and change its value.
str = ' { "a":1, "b":2 } ';
result = JSON.parse (str, function (k, v) {
if (k == "a")
{
Object.defineProperty(this, "b", {
enumerable: true,
// FIXME: Should work with configurable: true.
configurable: false,
writable: false,
value: 2
});
return 8;
}
if (k == "b")
{
return 9;
}
return v;
});
assert (result.a === 8);
assert (result.b === 2);
// Throw error in the reviver
try {
str = ' { "a":1, "b":2 } ';
result = JSON.parse (str, function (k, v) { throw new ReferenceError("error"); } );
assert(false);
} catch (e) {
assert (e.message === "error");
assert (e instanceof ReferenceError);
}
// Throw error in a getter
try {
str = ' { "a":1, "b":2 } ';
JSON.parse (str, function (k, v) {
if (k == "a")
{
Object.defineProperty(this, "b", {
enumerable: true,
configurable: true,
get: function() { throw new ReferenceError("error"); }
});
}
return v;
});
assert(false);
} catch (e) {
assert (e.message === "error");
assert (e instanceof ReferenceError);
}
// Checking reviver with different primitive types
str = ' { "a":1 } ';
result = JSON.parse (str, 4);
assert (result.a == 1);
result = JSON.parse (str, null);
assert (result.a == 1);
result = JSON.parse (str, undefined);
assert (result.a == 1);
result = JSON.parse (str, true);
assert (result.a == 1);
result = JSON.parse (str, "foo");
assert (result.a == 1);
// Checking reviver with different object types
str = ' { "a":1 } ';
result = JSON.parse(str, new Boolean (true));
assert (result.a == 1);
result = JSON.parse(str, new String ("foo"));
assert (result.a == 1);
result = JSON.parse(str, new Number (3));
assert (result.a == 1);
result = JSON.parse(str, {"a": 2});
assert (result.a == 1);
result = JSON.parse(str, [1, 2, 3]);
assert (result.a == 1);
+186
View File
@@ -0,0 +1,186 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Checking quoting strings
normal_string = "asdasd";
assert (JSON.stringify (normal_string) == '"asdasd"');
format_characters = "\ba\fs\nd\ra\tsd";
assert (JSON.stringify (format_characters) == '"\\ba\\fs\\nd\\ra\\tsd"');
ctl_string = "asdasd";
assert (JSON.stringify (ctl_string) == '"asd\\u001fasd"');
escpad_string = "\"asda\sd";
assert (JSON.stringify (escpad_string) == '"\\"asdasd"');
// Checking primitive types
assert (JSON.stringify (1) === '1');
assert (JSON.stringify (true) === 'true');
assert (JSON.stringify ("foo") === '"foo"');
assert (JSON.stringify (null) === 'null');
assert (JSON.stringify (undefined) === undefined);
assert (JSON.stringify (new Number(1)) === '1');
assert (JSON.stringify (new Boolean(true)) === 'true');
assert (JSON.stringify (new String("foo")) === '"foo"');
// Checking objects
empty_object = {}
assert (JSON.stringify (empty_object) == '{}');
empty_object = {};
empty_object.a = undefined;
assert (JSON.stringify (empty_object) == '{}');
p_object = { "a": 1, "b": true, "c": "foo", "d": null, "e": undefined };
assert (JSON.stringify (p_object) == '{"d":null,"c":"foo","b":true,"a":1}');
o_object = { "a": new Number(1), "b": new Boolean(true), "c": new String("foo") };
assert (JSON.stringify (o_object) == '{"c":"foo","b":true,"a":1}');
child = { "a": 1, "b": new String("\nfoo"), "c": undefined };
parent = { "a": true, "b": child, "c": null};
assert (JSON.stringify (parent) == '{"c":null,"b":{"b":"\\nfoo","a":1},"a":true}');
recursive_object = {};
recursive_object.a = 2;
recursive_object.b = recursive_object;
try {
JSON.stringify (recursive_object)
// Should not be reached.
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
// Checking arrays
empty_array = [];
assert (JSON.stringify (empty_array) == '[]');
array = [undefined];
assert (JSON.stringify (array) == '[null]');
p_array = [1, true, "foo", null, undefined];
assert (JSON.stringify (p_array) == '[1,true,"foo",null,null]');
o_array = [new Number(1), new Boolean(true), new String("foo")];
assert (JSON.stringify (o_array) == '[1,true,"foo"]');
child = [ 1, new String("\nfoo"), undefined ];
parent = [ true, child, null ];
assert (JSON.stringify (parent) == '[true,[1,"\\nfoo",null],null]');
recursive_array = [];
recursive_array[0] = 2;
recursive_array[1] = recursive_array;
try {
JSON.stringify (recursive_array)
// Should not be reached.
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
object = {"a": 1, "b": [1, true, {"a": "foo"}]};
assert (JSON.stringify (object) == '{"b":[1,true,{"a":"foo"}],"a":1}');
array = [1, {"a": 2, "b": true, c: [3]}];
assert (JSON.stringify (array) == '[1,{"c":[3],"b":true,"a":2}]');
// Filtering / replacing
to_json_object = {};
to_json_object.a = 2;
to_json_object.toJSON = function (key) { return 3; };
assert (JSON.stringify (to_json_object) === "3");
function replacer_function (key, value)
{
if (typeof(value) == "string")
return "FOO";
return value;
}
object = { "a": "JSON", "b": new String("JSON"), "c": 3 };
assert (JSON.stringify (object, replacer_function) == '{"c":3,"b":"JSON","a":"FOO"}');
filter = ["a", "b"];
assert (JSON.stringify (object, filter) == '{"a":"JSON","b":"JSON"}');
// Throw error in the replacer function
function replacer_thrower (key, value)
{
throw new ReferenceError("foo");
return value;
}
try {
JSON.stringify (object, replacer_thrower)
// Should not be reached.
assert (false);
} catch (e) {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}
// Checking replacer with different primitive types
object = { "a": 2 };
assert (JSON.stringify (object, 3) == '{"a":2}');
assert (JSON.stringify (object, true) == '{"a":2}');
assert (JSON.stringify (object, null) == '{"a":2}');
assert (JSON.stringify (object, undefined) == '{"a":2}');
assert (JSON.stringify (object, "foo") == '{"a":2}');
// Checking replacer with different primitive types
assert (JSON.stringify (object, new Boolean (true)) == '{"a":2}');
assert (JSON.stringify (object, new Number (3)) == '{"a":2}');
assert (JSON.stringify (object, new String ("foo")) == '{"a":2}');
assert (JSON.stringify (object, { "a": 3 }) == '{"a":2}');
// Checking JSON formatting
object = {"a": 2};
assert (JSON.stringify (object, null, " ") == '{\n "a": 2\n}');
assert (JSON.stringify (object, null, "asd") == '{\nasd"a": 2\n}');
assert (JSON.stringify (object, null, "asd0123456789") == '{\nasd0123456"a": 2\n}');
assert (JSON.stringify (object, null, 100) == '{\n "a": 2\n}');
array = [2];
assert (JSON.stringify (array, null, " ") == '[\n 2\n]');
assert (JSON.stringify (array, null, "asd") == '[\nasd2\n]');
assert (JSON.stringify (array, null, "asd0123456789") == '[\nasd01234562\n]');
assert (JSON.stringify (array, null, 100) == '[\n 2\n]');
nested_object = {"a": 2, "b": {"c": 1, "d": true}};
assert (JSON.stringify (nested_object, null, 2) == '{\n "b": {\n "d": true,\n "c": 1\n },\n "a": 2\n}');
nested_array = [2, [1,true]];
assert (JSON.stringify (nested_array, null, 2) == '[\n 2,\n [\n 1,\n true\n ]\n]');
// Checking space (formatting parameter) with different primititve types
object = { "a": 2 };
assert (JSON.stringify (object, null, true) == '{"a":2}');
assert (JSON.stringify (object, null, null) == '{"a":2}');
assert (JSON.stringify (object, null, undefined) == '{"a":2}');
// Checking space (formatting parameter) with different object types
assert (JSON.stringify (object, null, new Boolean (true)) == '{"a":2}');
assert (JSON.stringify (object, null, [1, 2, 3] ) == '{"a":2}');
assert (JSON.stringify (object, null, { "a": 3 }) == '{"a":2}');