-
-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Scala Native #826
Merged
Merged
Support Scala Native #826
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
61a1d5e
Support Scala Native
lolgab a366786
Implement Scala Native parser using jawn
lolgab f8854b9
Update Scala Native to 0.4.9
lolgab 25f923d
Fix string escaping for Scala 2.12
lolgab 5f12891
Add header to SecureRandom
lolgab f2c12d6
Run Scalafmt
lolgab a07c923
Share EnumSpec with JVM since native has the correct behavior
lolgab 90c869f
Avoid stringify local function
lolgab 13fa246
Increase memory limits in CI
lolgab 1dd2df4
Rename StaticBindingJsNative to StaticBindingNonJvm
lolgab f05f1ce
Upgrade versions
mkurz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
113 changes: 113 additions & 0 deletions
113
play-json/js-native/src/main/scala/StaticBindingNonJvm.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (C) 2009-2021 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package play.api.libs.json | ||
|
||
import java.io.InputStreamReader | ||
|
||
private[json] object StaticBindingNonJvm { | ||
|
||
/** Parses a [[JsValue]] from a stream (assuming UTF-8). */ | ||
def parseJsValue(stream: java.io.InputStream): JsValue = { | ||
var in: InputStreamReader = null | ||
|
||
try { | ||
in = new java.io.InputStreamReader(stream, "UTF-8") | ||
val acc = new StringBuilder() | ||
val buf = Array.ofDim[Char](1024) | ||
|
||
@annotation.tailrec | ||
def read(): String = { | ||
val r = in.read(buf, 0, 1024) | ||
|
||
if (r == 1024) { | ||
acc ++= buf | ||
read() | ||
} else if (r > 0) { | ||
acc ++= buf.slice(0, r) | ||
read() | ||
} else acc.result() | ||
} | ||
|
||
StaticBinding.parseJsValue(read()) | ||
} catch { | ||
case err: Throwable => throw err | ||
} finally { | ||
if (in != null) in.close() | ||
} | ||
} | ||
|
||
def generateFromJsValue(jsValue: JsValue, escapeNonASCII: Boolean): String = | ||
fromJs(jsValue, escapeNonASCII, 0, _ => "") | ||
|
||
def prettyPrint(jsValue: JsValue): String = | ||
fromJs( | ||
jsValue, | ||
false, | ||
0, | ||
{ l => | ||
0.until(l * 2).map(_ => ' ').mkString | ||
}, | ||
newline = true, | ||
fieldValueSep = " : ", | ||
arraySep = ("[ ", ", ", " ]") | ||
) | ||
|
||
def toBytes(jsValue: JsValue): Array[Byte] = | ||
generateFromJsValue(jsValue, false).getBytes("UTF-8") | ||
|
||
def fromJs( | ||
jsValue: JsValue, | ||
escapeNonASCII: Boolean, | ||
ilevel: Int, | ||
indent: Int => String, | ||
newline: Boolean = false, | ||
fieldValueSep: String = ":", | ||
arraySep: (String, String, String) = ("[", ",", "]") | ||
): String = { | ||
def str = jsValue match { | ||
case JsNull => "null" | ||
case JsString(s) => StaticBinding.fromString(s, escapeNonASCII) | ||
case JsNumber(n) => n.toString | ||
case JsTrue => "true" | ||
case JsFalse => "false" | ||
|
||
case JsArray(items) => { | ||
val il = ilevel + 1 | ||
|
||
items | ||
.map(fromJs(_, escapeNonASCII, il, indent, newline, fieldValueSep, arraySep)) | ||
.mkString(arraySep._1, arraySep._2, arraySep._3) | ||
} | ||
|
||
case JsObject(fields) => { | ||
val il = ilevel + 1 | ||
val (before, after) = if (newline) { | ||
s"\n${indent(il)}" -> s"\n${indent(ilevel)}}" | ||
} else indent(il) -> "}" | ||
|
||
fields | ||
.map { case (k, v) => | ||
@inline def key = StaticBinding.fromString(k, escapeNonASCII) | ||
@inline def value = fromJs(v, escapeNonASCII, il, indent, newline, fieldValueSep, arraySep) | ||
|
||
s"$before$key$fieldValueSep$value" | ||
} | ||
.mkString("{", ",", after) | ||
} | ||
} | ||
|
||
str | ||
} | ||
|
||
def escapeStr(s: String): String = s.flatMap { c => | ||
val code = c.toInt | ||
|
||
if (code > 31 && code < 127 /* US-ASCII */ ) String.valueOf(c) | ||
else { | ||
def hexCode = code.toHexString.reverse.padTo(4, '0').reverse | ||
'\\' +: s"u${hexCode.toUpperCase}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2009-2021 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package play.api.libs.json | ||
|
||
import org.typelevel.jawn | ||
|
||
object StaticBinding { | ||
|
||
private implicit object JsValueFacade extends jawn.Facade.SimpleFacade[JsValue] { | ||
final def jfalse: JsValue = JsFalse | ||
final def jnull: JsValue = JsNull | ||
final def jnum(s: CharSequence, decIndex: Int, expIndex: Int): JsValue = JsNumber( | ||
new java.math.BigDecimal(s.toString) | ||
) | ||
final def jstring(s: CharSequence): JsValue = JsString(s.toString) | ||
final def jtrue: JsValue = JsTrue | ||
|
||
final def jarray(vs: List[JsValue]): JsValue = JsArray(vs) | ||
final def jobject(vs: Map[String, JsValue]): JsValue = JsObject(vs) | ||
} | ||
|
||
/** Parses a [[JsValue]] from raw data (assuming UTF-8). */ | ||
def parseJsValue(data: Array[Byte]): JsValue = | ||
new jawn.ByteArrayParser[JsValue](data).parse() | ||
|
||
/** Parses a [[JsValue]] from a string content. */ | ||
def parseJsValue(input: String): JsValue = | ||
jawn.Parser.parseUnsafe[JsValue](input) | ||
|
||
/** Parses a [[JsValue]] from a stream (assuming UTF-8). */ | ||
def parseJsValue(stream: java.io.InputStream): JsValue = | ||
StaticBindingNonJvm.parseJsValue(stream) | ||
|
||
def generateFromJsValue(jsValue: JsValue, escapeNonASCII: Boolean): String = | ||
StaticBindingNonJvm.generateFromJsValue(jsValue, escapeNonASCII) | ||
|
||
def prettyPrint(jsValue: JsValue): String = StaticBindingNonJvm.prettyPrint(jsValue) | ||
|
||
def toBytes(jsValue: JsValue): Array[Byte] = StaticBindingNonJvm.toBytes(jsValue) | ||
|
||
@inline private[json] def fromString(s: String, escapeNonASCII: Boolean): String = { | ||
def escaped(c: Char) = c match { | ||
case '\b' => "\\b" | ||
case '\f' => "\\f" | ||
case '\n' => "\\n" | ||
case '\r' => "\\r" | ||
case '\t' => "\\t" | ||
case '\\' => "\\\\" | ||
case '\"' => "\\\"" | ||
case c => c.toString | ||
} | ||
val stringified = if (s == null) "null" else s""""${s.flatMap(escaped)}"""" | ||
cchantep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!escapeNonASCII) stringified else StaticBindingNonJvm.escapeStr(stringified) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figure this is needed to ensure linting works on the new rules. But this isn't really associated with Lightbend anymore. @mkurz we should visit revamping the requirements on copyrights and file headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do that in a upcoming pr.