Skip to content

Commit

Permalink
bump uJson version to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi-databricks committed Mar 18, 2021
1 parent 9544aad commit 9e001b5
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mill._, scalalib._, publish._, scalajslib._, scalanativelib._, scalanativelib.api._
val sjsonnetVersion = "0.3.1"
val sjsonnetVersion = "0.3.2"

object sjsonnet extends Cross[SjsonnetModule]("2.12.13", "2.13.4")
class SjsonnetModule(val crossScalaVersion: String) extends Module {
Expand All @@ -18,7 +18,7 @@ class SjsonnetModule(val crossScalaVersion: String) extends Module {
def ivyDeps = Agg(
ivy"com.lihaoyi::fastparse::2.3.1",
ivy"com.lihaoyi::pprint::0.6.1",
ivy"com.lihaoyi::ujson::1.2.3",
ivy"com.lihaoyi::ujson::1.3.0",
ivy"com.lihaoyi::scalatags::0.9.3",
ivy"org.scala-lang.modules::scala-collection-compat::2.4.0"
)
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ To publish, run the following commands:

## Changelog

### 0.3.2

- Bump uJson version to 1.3.0

### 0.3.1

- Avoid catching fatal exceptions during evaluation
Expand Down
155 changes: 155 additions & 0 deletions sjsonnet/src/sjsonnet/BaseRenderer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package sjsonnet
import upickle.core.{ObjVisitor, ArrVisitor}
import scala.annotation.switch

/**
* Vendored version of `ujson.BaseRenderer` from ujson 1.2.3.
*
* uJson has replaced this with a pair of byte/char specialized renderers for
* performance. For now, we just want to upgrade uJson to the latest version
* to avoid classpath conflicts, so just vendor this code for now. In the future
* we may remove it to interface with uJson's specialized renderers directly, to
* benefit from their improved performance.
*/
class BaseRenderer[T <: java.io.Writer]
(out: T,
indent: Int = -1,
escapeUnicode: Boolean = false) extends ujson.JsVisitor[T, T]{
var depth: Int = 0
val colonSnippet = if (indent == -1) ":" else ": "

var commaBuffered = false

def flushBuffer() = {
if (commaBuffered) {
commaBuffered = false
out.append(',')
renderIndent()
}
}
def visitArray(length: Int, index: Int) = new ArrVisitor[T, T] {
flushBuffer()
out.append('[')

depth += 1
renderIndent()
def subVisitor = BaseRenderer.this
def visitValue(v: T, index: Int): Unit = {
flushBuffer()
commaBuffered = true
}
def visitEnd(index: Int) = {
commaBuffered = false
depth -= 1
renderIndent()
out.append(']')
out
}
}

def visitObject(length: Int, index: Int) = new ObjVisitor[T, T] {
flushBuffer()
out.append('{')
depth += 1
renderIndent()
def subVisitor = BaseRenderer.this
def visitKey(index: Int) = BaseRenderer.this
def visitKeyValue(s: Any): Unit = out.append(colonSnippet)
def visitValue(v: T, index: Int): Unit = {
commaBuffered = true
}
def visitEnd(index: Int) = {
commaBuffered = false
depth -= 1
renderIndent()
out.append('}')
out
}
}

def visitNull(index: Int) = {
flushBuffer()
out.append("null")
out
}

def visitFalse(index: Int) = {
flushBuffer()
out.append("false")
out
}

def visitTrue(index: Int) = {
flushBuffer()
out.append("true")
out
}

def visitFloat64StringParts(s: CharSequence, decIndex: Int, expIndex: Int, index: Int) = {
flushBuffer()
out.append(s)
out
}

override def visitFloat64(d: Double, index: Int) = {
d match{
case Double.PositiveInfinity => visitString("Infinity", -1)
case Double.NegativeInfinity => visitString("-Infinity", -1)
case d if java.lang.Double.isNaN(d) => visitString("NaN", -1)
case d =>
val i = d.toInt
if (d == i) visitFloat64StringParts(i.toString, -1, -1, index)
else super.visitFloat64(d, index)
flushBuffer()
}

out
}

def visitString(s: CharSequence, index: Int) = {
flushBuffer()
if (s == null) out.append("null")
else BaseRenderer.escape(out, s, escapeUnicode)

out
}

final def renderIndent() = {
if (indent == -1) ()
else {
out.append('\n')
var i = indent * depth
while(i > 0) {
out.append(' ')
i -= 1
}
}
}
}
object BaseRenderer {
final def escape(sb: java.io.Writer, s: CharSequence, unicode: Boolean): Unit = {
sb.append('"')
var i = 0
val len = s.length
while (i < len) {
(s.charAt(i): @switch) match {
case '"' => sb.append("\\\"")
case '\\' => sb.append("\\\\")
case '\b' => sb.append("\\b")
case '\f' => sb.append("\\f")
case '\n' => sb.append("\\n")
case '\r' => sb.append("\\r")
case '\t' => sb.append("\\t")
case c =>
if (c < ' ' || (c > '~' && unicode)) {
sb.append("\\u").append(toHex((c >> 12) & 15)).append(toHex((c >> 8) & 15))
.append(toHex((c >> 4) & 15)).append(toHex(c & 15))
} else sb.append(c)
}
i += 1
}
sb.append('"')
}

private def toHex(nibble: Int): Char = (nibble + (if (nibble >= 10) 87 else 48)).toChar
}
3 changes: 1 addition & 2 deletions sjsonnet/src/sjsonnet/PrettyYamlRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sjsonnet
import java.io.{StringWriter, Writer}
import java.util.regex.Pattern

import ujson.BaseRenderer
import upickle.core.{ArrVisitor, ObjVisitor}
import fastparse.IndexedParserInput

Expand Down Expand Up @@ -69,7 +68,7 @@ class PrettyYamlRenderer(out: Writer = new java.io.StringWriter(),
// or have leading/trailing spaces, are rendered single-quoted
else if (PrettyYamlRenderer.stringNeedsToBeQuoted(str)) {
val strWriter = new StringWriter
ujson.Renderer.escape(strWriter, s, unicode = true)
BaseRenderer.escape(strWriter, s, unicode = true)
val quotedStr = "'" + str.replace("'", "''") + "'"
PrettyYamlRenderer.writeWrappedString(quotedStr, leftHandPrefixOffset, out, indent * (depth + 1), idealWidth)
leftHandPrefixOffset = quotedStr.length + 2
Expand Down
1 change: 0 additions & 1 deletion sjsonnet/src/sjsonnet/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sjsonnet
import java.io.Writer

import upickle.core.{ArrVisitor, ObjVisitor}
import ujson.BaseRenderer

/**
* Custom JSON renderer to try and match the behavior of google/jsonnet's
Expand Down
2 changes: 1 addition & 1 deletion sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ object Std {
},
builtin("escapeStringJson", "str"){ (offset, ev, fs, str: String) =>
val out = new StringWriter()
ujson.Renderer.escape(out, str, unicode = true)
BaseRenderer.escape(out, str, unicode = true)
out.toString
},
builtin("escapeStringBash", "str"){ (offset, ev, fs, str: String) =>
Expand Down
3 changes: 1 addition & 2 deletions sjsonnet/src/sjsonnet/YamlRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.io.{StringWriter, Writer}
import java.util.regex.Pattern

import upickle.core.{ArrVisitor, ObjVisitor}
import ujson.BaseRenderer



Expand Down Expand Up @@ -34,7 +33,7 @@ class YamlRenderer(out: StringWriter = new java.io.StringWriter(), indentArrayIn
depth -= 1
out
} else {
ujson.Renderer.escape(out, s, unicode = true)
BaseRenderer.escape(out, s, unicode = true)
out
}
}
Expand Down

0 comments on commit 9e001b5

Please sign in to comment.