Skip to content

Commit

Permalink
Switch to direct compact rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Oct 24, 2024
1 parent 336ccc3 commit 391b2cc
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion json/src/main/scala/fs2/data/json/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,71 @@ package object json {
* You can use this to write the Json stream to a file.
*/
def compact[F[_]]: Pipe[F, Token, String] =
_.through(fs2.data.text.render.pretty(width = Int.MaxValue)(Token.compact))
_.scanChunks((0, false)) { case (state, chunk) =>
val builder = new StringBuilder
val state1 =
chunk.foldLeft(state) {
case ((level, comma), Token.StartObject) =>
if (comma) {
builder.append(',')
}
builder.append(('{'))
(level + 1, false)
case ((level, _), Token.EndObject) =>
builder.append('}')
(level - 1, level > 1)
case ((level, comma), Token.StartArray) =>
if (comma) {
builder.append(',')
}
builder.append(('['))
(level + 1, false)
case ((level, _), Token.EndArray) =>
builder.append(']')
(level - 1, level > 1)
case ((level, comma), Token.Key(key)) =>
if (comma) {
builder.append(',')
}
builder.append('"')
Token.renderString(key, 0, builder)
builder.append("\":")
(level, false)
case ((level, comma), Token.StringValue(key)) =>
if (comma) {
builder.append(',')
}
builder.append('"')
Token.renderString(key, 0, builder)
builder.append('"')
(level, level > 0)
case ((level, comma), Token.NumberValue(n)) =>
if (comma) {
builder.append(',')
}
builder.append(n)
(level, level > 0)
case ((level, comma), Token.TrueValue) =>
if (comma) {
builder.append(',')
}
builder.append("true")
(level, level > 0)
case ((level, comma), Token.FalseValue) =>
if (comma) {
builder.append(',')
}
builder.append("false")
(level, level > 0)
case ((level, comma), Token.NullValue) =>
if (comma) {
builder.append(',')
}
builder.append("null")
(level, level > 0)
}
(state1, Chunk.singleton(builder.result()))
}

/** Renders a pretty-printed representation of the token stream with the given
* indentation size.
Expand Down

0 comments on commit 391b2cc

Please sign in to comment.