File tree 4 files changed +39
-44
lines changed
4 files changed +39
-44
lines changed Original file line number Diff line number Diff line change @@ -73,45 +73,9 @@ pub fn Float::to_json(self : Float) -> Json {
73
73
Number (self .to_double ())
74
74
}
75
75
76
- ///|
77
- fn escape_json_string (str : String ) -> String {
78
- fn to_hex_digit (i : Int ) -> Char {
79
- if i < 10 {
80
- Char ::from_int ('0' .to_int () + i )
81
- } else {
82
- Char ::from_int ('a' .to_int () + (i - 10 ))
83
- }
84
- }
85
-
86
- let len = str .length ()
87
- let buf = StringBuilder ::new (size_hint = len )
88
- for i in 0..< len {
89
- let c = str [i ]
90
- match c {
91
- '\n ' => buf .write_string ("\\ n" )
92
- '\r ' => buf .write_string ("\\ r" )
93
- '\b ' => buf .write_string ("\\ b" )
94
- '\t ' => buf .write_string ("\\ t" )
95
- _ => {
96
- let code = c .to_int ()
97
- if code == 0x0C {
98
- buf .write_string ("\\ f" )
99
- } else if code < 0x20 {
100
- buf .write_string ("\\ u00" )
101
- buf .write_char (to_hex_digit (code / 16 ))
102
- buf .write_char (to_hex_digit (code % 16 ))
103
- } else {
104
- buf .write_char (c )
105
- }
106
- }
107
- }
108
- }
109
- buf .to_string ()
110
- }
111
-
112
76
///|
113
77
pub fn String ::to_json (self : String ) -> Json {
114
- String (escape_json_string ( self ) )
78
+ String (self )
115
79
}
116
80
117
81
///|
Original file line number Diff line number Diff line change @@ -104,21 +104,36 @@ test "test UInt64::to_json" {
104
104
test "escape control characters" {
105
105
let str = "abc\x01 def" // Control character with code 1
106
106
let json = str .to_json ()
107
- inspect! (json , content = "String(\" abc\\\\ u0001def\" )" )
107
+ inspect! (
108
+ json ,
109
+ content =
110
+ #| String("abc\x01def")
111
+ ,
112
+ )
108
113
}
109
114
110
115
///|
111
116
test "test carriage return and backspace" {
112
117
let test_string = "CR\r BS\b "
113
118
let json = test_string .to_json ()
114
- inspect! (json , content = "String(\" CR\\\\ rBS\\\\ b\" )" )
119
+ inspect! (
120
+ json ,
121
+ content =
122
+ #| String("CR\rBS\b")
123
+ ,
124
+ )
115
125
}
116
126
117
127
///|
118
128
test "test form feed" {
119
129
let test_string = "Form\u000C Feed"
120
130
let json = test_string .to_json ()
121
- inspect! (json , content = "String(\" Form\\\\ fFeed\" )" )
131
+ inspect! (
132
+ json ,
133
+ content =
134
+ #| String("Form\x0cFeed")
135
+ ,
136
+ )
122
137
}
123
138
124
139
///|
@@ -143,7 +158,7 @@ test "Bool::to_json true" {
143
158
test "to_hex_digit" {
144
159
let str = "\n\r\b\t\x0C\x00 "
145
160
let escaped = str .to_json ().as_string ().unwrap ()
146
- inspect! (escaped , content = "\\ n \\ r \\ b \\ t \\ f \\ u0000 " )
161
+ inspect! (escaped , content = "\n\r\b\t\x0c\x00 " )
147
162
}
148
163
149
164
///|
Original file line number Diff line number Diff line change @@ -347,6 +347,22 @@ test "stringify escape round trip" {
347
347
}
348
348
}
349
349
350
+ ///|
351
+ test "string from and to json round trip" {
352
+ let greeting =
353
+ #|
354
+ #| (\(\
355
+ #| ( -.-)
356
+ #| o_(")(")
357
+ #| __ __ ____ __ ___ ____ _ __
358
+ #| / / / /__ / / /___ / |/ /___ ____ ____ / __ )(_) /_
359
+ #| / /_/ / _ \/ / / __ \ / /|_/ / __ \/ __ \/ __ \/ __ / / __/
360
+ #| / __ / __/ / / /_/ / / / / / /_/ / /_/ / / / / /_/ / / /_
361
+ #| /_/ /_/\___/_/_/\____/ /_/ /_/\____/\____/_/ /_/_____/_/\__/
362
+ #|
363
+ assert_eq! (@json .from_json! (greeting .to_json ()), greeting )
364
+ }
365
+
350
366
///|
351
367
test "escape" {
352
368
let s = "http://example.com/"
Original file line number Diff line number Diff line change @@ -102,19 +102,19 @@ test "String::to_json" {
102
102
inspect! (
103
103
"\x00 " .to_json (),
104
104
content =
105
- #| String("\\u0000 ")
105
+ #| String("\x00 ")
106
106
,
107
107
)
108
108
inspect! (
109
109
"\n\r\b\t\\ " .to_json (),
110
110
content =
111
- #| String("\\n\\r\\b\ \t\\")
111
+ #| String("\n\r\b \t\\")
112
112
,
113
113
)
114
114
inspect! (
115
115
"\x0c\x0d\x0f " .to_json (),
116
116
content =
117
- #| String("\\f\\r\\u000f ")
117
+ #| String("\x0c\r\x0f ")
118
118
,
119
119
)
120
120
}
You can’t perform that action at this time.
0 commit comments