Skip to content

Commit 0927339

Browse files
committed
impl ToJson for BytesView and Bytes
1 parent 59f6cfe commit 0927339

File tree

7 files changed

+41
-22
lines changed

7 files changed

+41
-22
lines changed

builtin/json.mbt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,6 @@ pub impl ToJson for Byte with to_json(self : Byte) -> Json {
177177
Json::number(self.to_double())
178178
}
179179
180-
///|
181-
/// Converts a `Bytes` value to a JSON representation.
182-
/// The representation is picked for easier debugging.
183-
/// Printable ASCII characters (from space to tilde, excluding '"' and '\') are output as-is.
184-
/// All other bytes are represented as \xHH, where HH is the two-digit hexadecimal value of the byte.
185-
pub impl ToJson for Bytes with to_json(self : Bytes) -> Json {
186-
let sb = StringBuilder::new()
187-
for b in self {
188-
if b is (b' '..=b'~') && b != b'"' && b != b'\\' {
189-
sb.write_char(b.to_char())
190-
} else {
191-
sb.write_string("\\x")
192-
sb.write_char(to_hex_digit(b.to_int() / 16))
193-
sb.write_char(to_hex_digit(b.to_int() % 16))
194-
}
195-
}
196-
Json::string(sb.to_string())
197-
}
198-
199180
///|
200181
pub impl ToJson for Int with to_json(self : Int) -> Json {
201182
Json::number(self.to_double())

builtin/pkg.generated.mbti

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,6 @@ impl ToJson for String
848848
impl[T : ToJson] ToJson for T?
849849
impl[Ok : ToJson, Err : ToJson] ToJson for Result[Ok, Err]
850850
impl[X : ToJson] ToJson for FixedArray[X]
851-
impl ToJson for Bytes
852851
impl[A : ToJson, B : ToJson] ToJson for (A, B)
853852
impl[A : ToJson, B : ToJson, C : ToJson] ToJson for (A, B, C)
854853
impl[A : ToJson, B : ToJson, C : ToJson, D : ToJson] ToJson for (A, B, C, D)

bytes/moon.pkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"moonbitlang/core/uint64",
88
"moonbitlang/core/test",
99
"moonbitlang/core/quickcheck",
10-
"moonbitlang/core/error"
10+
"moonbitlang/core/error",
11+
"moonbitlang/core/json"
1112
]
1213
}

bytes/pkg.generated.mbti

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn Bytes::unsafe_extract_uint_le(Bytes, Int, Int) -> UInt
3434
impl Add for Bytes
3535
impl Default for Bytes
3636
impl Hash for Bytes
37+
impl ToJson for Bytes
3738

3839
#alias("_[_]")
3940
fn BytesView::at(Self, Int) -> Byte
@@ -71,6 +72,7 @@ impl Compare for BytesView
7172
impl Eq for BytesView
7273
impl Hash for BytesView
7374
impl Show for BytesView
75+
impl ToJson for BytesView
7476

7577
// Type aliases
7678
pub typealias BytesView as View

bytes/view.mbt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,36 @@ pub impl Hash for BytesView with hash_combine(self : BytesView, hasher : Hasher)
653653
pub impl Hash for BytesView with hash(self : BytesView) -> Int {
654654
xxhash32(self.data(), 0, offset=self.start_offset(), len=self.length())
655655
}
656+
657+
///|
658+
fn to_hex_digit(i : Int) -> Char {
659+
if i < 10 {
660+
(i + '0').unsafe_to_char()
661+
} else {
662+
(i + 'a' - 10).unsafe_to_char()
663+
}
664+
}
665+
666+
///|
667+
pub impl ToJson for BytesView with to_json(self) -> Json {
668+
let sb = StringBuilder::new()
669+
for b in self {
670+
if b is (b' '..=b'~') && b != b'"' && b != b'\\' {
671+
sb.write_char(b.to_char())
672+
} else {
673+
sb.write_string("\\x")
674+
sb.write_char(to_hex_digit(b.to_int() / 16))
675+
sb.write_char(to_hex_digit(b.to_int() % 16))
676+
}
677+
}
678+
Json::string(sb.to_string())
679+
}
680+
681+
///|
682+
/// Converts a `Bytes` value to a JSON representation.
683+
/// The representation is picked for easier debugging.
684+
/// Printable ASCII characters (from space to tilde, excluding '"' and '\') are output as-is.
685+
/// All other bytes are represented as \xHH, where HH is the two-digit hexadecimal value of the byte.
686+
pub impl ToJson for Bytes with to_json(self : Bytes) -> Json {
687+
BytesView::to_json(self[:])
688+
}

bytes/view_test.mbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ test "basic1" {
2323
inspect(bv2, content="b\"\\x00\\x01\\x02\\x03\\x04\\x05\"")
2424
inspect(bv3, content="b\"\\x01\\x02\\x03\\x04\\x05\"")
2525
inspect(bv4, content="b\"\\x00\\x01\\x02\\x03\"")
26+
@json.inspect(bv1, content="\\x01\\x02\\x03")
27+
@json.inspect(b"abc"[:], content="abc")
2628
}
2729

2830
///|

json/moon.pkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"moonbitlang/core/result",
1313
"moonbitlang/core/unit",
1414
"moonbitlang/core/array",
15-
"moonbitlang/core/bigint"
15+
"moonbitlang/core/bigint",
16+
"moonbitlang/core/bytes"
1617
]
1718
}

0 commit comments

Comments
 (0)