-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path025_json.xml
More file actions
136 lines (103 loc) · 3.42 KB
/
025_json.xml
File metadata and controls
136 lines (103 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?xml version="1.0" encoding="utf-8" ?>
<!--
/*****************************************************************************
*
* ReoScript - .NET Script Language Engine
*
* https://github.com/unvell/ReoScript
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
*
* This software released under MIT license.
* Copyright (c) 2012-2019 Jingwood, unvell.com, all rights reserved.
*
****************************************************************************/
-->
<test-suite id="025" name="json">
<test-case id="001" name="parse">
<script>
var t = debug.assert;
var src = "{name: 'apple', color: 'red', count: 10}";
// JSON.parse usage:
//
// parse( JSON_literal )
// parse( JSON_literal, converter )
//
// where converter is:
// function(key, value) { return value; }
// or in lambda:
// (key, value) => value
//
var obj = JSON.parse(src, (key, value) => {
return value;
});
testParsedObject(obj);
obj = JSON.parse(src); // converter is null
testParsedObject(obj);
function testParsedObject(obj) {
t( obj != null );
t( typeof obj, 'object' );
t( obj instanceof Object );
t( obj.hasOwnProperty('name'), true );
t( obj.hasOwnProperty('color'), true );
t( obj.hasOwnProperty('count'), true );
t( obj.name, 'apple' );
t( obj.color, 'red' );
t( obj.count, 10 );
}
</script>
</test-case>
<test-case id="002" name="parse with converter">
<script>
var t = debug.assert;
var src = "{name: 'apple', color: 'red', count: 10}";
var obj = JSON.parse(src, (key, value) => {
if (key == 'color')
return null;
else
return value;
});
t( typeof obj, 'object' );
t( obj instanceof Object );
t( obj.hasOwnProperty('name'), true );
t( obj.hasOwnProperty('color'), true );
t( obj.hasOwnProperty('count'), true );
t( obj.name, 'apple' );
t( obj.color, null );
t( obj.count, 10 );
</script>
</test-case>
<test-case id="003" name="eval">
<script>
var t = debug.assert;
// JavaScript style
// eval('(' + json + ')');
var obj = eval("( {name:'apple', color:'red', eat: function(){}} )");
t( typeof obj, 'object' );
t( obj instanceof Object );
t( obj.name, 'apple' );
t( obj.color, 'red' );
t( typeof obj.eat, 'function' );
t( obj.eat(), null );
</script>
</test-case>
<test-case id="004" name="stringify">
<script>
var t = debug.assert;
var obj = {name:'apple', color:'red', amount: 5};
var str = JSON.stringify(obj);
t(str, '{name:"apple",color:"red",amount:5}');
</script>
</test-case>
<test-case id="005" name="stringify handler">
<script>
var t = debug.assert;
var obj = {name:'apple', color:'red', amount: 5};
var str = JSON.stringify(obj, (key, value) => String(value));
t(str, '{name:"apple",color:"red",amount:"5"}');
</script>
</test-case>
</test-suite>