Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions builtin/json.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,6 @@ pub impl ToJson for Byte with to_json(self : Byte) -> Json {
Json::number(self.to_double())
}

///|
/// Converts a `Bytes` value to a JSON representation.
/// The representation is picked for easier debugging.
/// Printable ASCII characters (from space to tilde, excluding '"' and '\') are output as-is.
/// All other bytes are represented as \xHH, where HH is the two-digit hexadecimal value of the byte.
pub impl ToJson for Bytes with to_json(self : Bytes) -> Json {
let sb = StringBuilder::new()
for b in self {
if b is (b' '..=b'~') && b != b'"' && b != b'\\' {
sb.write_char(b.to_char())
} else {
sb.write_string("\\x")
sb.write_char(to_hex_digit(b.to_int() / 16))
sb.write_char(to_hex_digit(b.to_int() % 16))
}
}
Json::string(sb.to_string())
}

///|
pub impl ToJson for Int with to_json(self : Int) -> Json {
Json::number(self.to_double())
Expand Down
1 change: 0 additions & 1 deletion builtin/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,6 @@ impl ToJson for String
impl[T : ToJson] ToJson for T?
impl[Ok : ToJson, Err : ToJson] ToJson for Result[Ok, Err]
impl[X : ToJson] ToJson for FixedArray[X]
impl ToJson for Bytes
impl[A : ToJson, B : ToJson] ToJson for (A, B)
impl[A : ToJson, B : ToJson, C : ToJson] ToJson for (A, B, C)
impl[A : ToJson, B : ToJson, C : ToJson, D : ToJson] ToJson for (A, B, C, D)
Expand Down
3 changes: 2 additions & 1 deletion bytes/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"moonbitlang/core/uint64",
"moonbitlang/core/test",
"moonbitlang/core/quickcheck",
"moonbitlang/core/error"
"moonbitlang/core/error",
"moonbitlang/core/json"
]
}
2 changes: 2 additions & 0 deletions bytes/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn Bytes::unsafe_extract_uint_le(Bytes, Int, Int) -> UInt
impl Add for Bytes
impl Default for Bytes
impl Hash for Bytes
impl ToJson for Bytes

#alias("_[_]")
fn BytesView::at(Self, Int) -> Byte
Expand Down Expand Up @@ -71,6 +72,7 @@ impl Compare for BytesView
impl Eq for BytesView
impl Hash for BytesView
impl Show for BytesView
impl ToJson for BytesView

// Type aliases
pub typealias BytesView as View
Expand Down
33 changes: 33 additions & 0 deletions bytes/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,36 @@ pub impl Hash for BytesView with hash_combine(self : BytesView, hasher : Hasher)
pub impl Hash for BytesView with hash(self : BytesView) -> Int {
xxhash32(self.data(), 0, offset=self.start_offset(), len=self.length())
}

///|
fn to_hex_digit(i : Int) -> Char {
if i < 10 {
(i + '0').unsafe_to_char()
} else {
(i + 'a' - 10).unsafe_to_char()
}
}

///|
pub impl ToJson for BytesView with to_json(self) -> Json {
let sb = StringBuilder::new()
for b in self {
if b is (b' '..=b'~') && b != b'"' && b != b'\\' {
sb.write_char(b.to_char())
} else {
sb.write_string("\\x")
sb.write_char(to_hex_digit(b.to_int() / 16))
sb.write_char(to_hex_digit(b.to_int() % 16))
}
}
Json::string(sb.to_string())
}

///|
/// Converts a `Bytes` value to a JSON representation.
/// The representation is picked for easier debugging.
/// Printable ASCII characters (from space to tilde, excluding '"' and '\') are output as-is.
/// All other bytes are represented as \xHH, where HH is the two-digit hexadecimal value of the byte.
pub impl ToJson for Bytes with to_json(self : Bytes) -> Json {
BytesView::to_json(self[:])
}
2 changes: 2 additions & 0 deletions bytes/view_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ test "basic1" {
inspect(bv2, content="b\"\\x00\\x01\\x02\\x03\\x04\\x05\"")
inspect(bv3, content="b\"\\x01\\x02\\x03\\x04\\x05\"")
inspect(bv4, content="b\"\\x00\\x01\\x02\\x03\"")
@json.inspect(bv1, content="\\x01\\x02\\x03")
@json.inspect(b"abc"[:], content="abc")
}

///|
Expand Down
3 changes: 2 additions & 1 deletion json/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"moonbitlang/core/result",
"moonbitlang/core/unit",
"moonbitlang/core/array",
"moonbitlang/core/bigint"
"moonbitlang/core/bigint",
"moonbitlang/core/bytes"
]
}
Loading