Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ lib_managed/
src_managed/
project/boot/
project/plugins/project/

.idea/
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sjson"
organization := "io.github.lock-free"
version := "0.2.0"
version := "0.2.5"
scalaVersion := "2.12.4"

useGpg := true
Expand All @@ -11,5 +11,7 @@ publishTo := sonatypePublishTo.value
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
// test suite
"org.scalatest" %% "scalatest" % "3.0.1" % Test
"org.scalatest" %% "scalatest" % "3.0.1" % Test,
//performance test suite
"com.storm-enroute" %% "scalameter" % "0.18"
)
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.3")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
40 changes: 38 additions & 2 deletions src/main/scala/io/github/shopee/idata/sjson/JSONUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,31 @@ object JSONUtil {
s""""${txtBuilder.toString()}""""
}

//returns a boolean array indicating where the start of a unicode substring is
//e.g. "\"\\uD835\" returns [False, True, False, False, False, False, False]
def uniCodeArrayBuilder(txt: String): Array[Boolean] = {
val unicodeArray = new Array[Boolean](txt.length)
val (allStringHexaDecimal, startPos) = (4, 3)
var (hexadecimalCount, startPtr) = (0, 0)
for (endPtr ← startPos to unicodeArray.length - 1){
startPtr = endPtr - 4
if(txt.charAt(endPtr).isDigit || isHexAlphabet(txt.charAt(endPtr))) hexadecimalCount = hexadecimalCount + 1
//startPtr starts from first possible hexadecimal character (e.g. Ds in "\uD835) that needs to be removed
//in a sliding window
if(startPtr >= 3 && (txt.charAt(startPtr).isDigit || isHexAlphabet(txt.charAt(startPtr)))) hexadecimalCount = hexadecimalCount - 1
if(hexadecimalCount == allStringHexaDecimal) unicodeArray.update(startPtr - 1, true)
}
unicodeArray
}

def isHexAlphabet(ch: Character): Boolean = {
if(ch >= 'A' && ch <= 'F') return true
false
}

def unescapeString(txt: String): String = {
val txtBuilder = new StringBuilder // use txt builder to collect text
val uniCodeArray = uniCodeArrayBuilder(txt)

var i = 1
var len = txt.length - 1
Expand All @@ -126,10 +149,23 @@ object JSONUtil {
case 'n' => '\n'
case 'f' => '\f'
case 'r' => '\r'
case 'u' => 'u'
case _ => next
}
txtBuilder.append(newChar)
i += 2
if(newChar == 'u' && uniCodeArray(i)) {
val unicodeString = s"\\u${txt.substring(i + 2, i + 6)}"
val unicodeChar = Integer.parseInt(unicodeString.drop(2), 16).toChar
txtBuilder.append(unicodeChar)
i += 6
}
else if (newChar == 'u') {
txtBuilder.append("\\u")
i += 2
}
else {
txtBuilder.append(newChar)
i += 2
}
} else {
txtBuilder.append(ch)
i += 1
Expand Down
32 changes: 32 additions & 0 deletions src/test/scala/io/github/shopee/idata/sjson/JSONUtil.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.free.lock.sjson

import org.scalatest.Matchers.{convertToAnyShouldWrapper, equal}

class JSONUtilTest extends org.scalatest.FunSuite {
test("unescapeString") {
assert(JSONUtil.unescapeString(JSONUtil.escapeString("1234")) == "1234")
Expand All @@ -10,4 +12,34 @@ class JSONUtilTest extends org.scalatest.FunSuite {
assert(JSONUtil.unescapeString(JSONUtil.escapeString("12\f34")) == "12\f34")
assert(JSONUtil.unescapeString(JSONUtil.escapeString("12\\34")) == "12\\34")
}

test("unicodeArrayBuilderSimple"){
val currString = "\"\\uD835\\uD83\""
val unicodeArray = new Array[Boolean](currString.length)
unicodeArray.update(1, true)
JSONUtil.uniCodeArrayBuilder(currString) should equal (unicodeArray)
}

test("unicodeArrayBuilderWithInvalidUniCodeInBetween2"){
val currString = "\"\\uD835\\uD835\\uD83\\uDC07\""
val unicodeArray = new Array[Boolean](currString.length)
unicodeArray.update(1, true)
unicodeArray.update(7, true)
unicodeArray.update(18, true)
JSONUtil.uniCodeArrayBuilder(currString) should equal (unicodeArray)
}

test("unicodeArrayBuilderWithInvalidUniCodeInBetween"){
val currString = "\"\\uD835\\uD83\\uD835\""
val unicodeArray = new Array[Boolean](currString.length)
unicodeArray.update(1, true)
unicodeArray.update(12, true)
JSONUtil.uniCodeArrayBuilder(currString) should equal (unicodeArray)
}

test("unicodeArrayNoUnicode"){
val currString = "\"\\uD83za\\uD83zxd\""
val unicodeArray = new Array[Boolean](currString.length)
JSONUtil.uniCodeArrayBuilder(currString) should equal (unicodeArray)
}
}
32 changes: 32 additions & 0 deletions src/test/scala/io/github/shopee/idata/sjson/ParseTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ class ParseTest extends org.scalatest.FunSuite {
assert(JSON.parse(JSON.stringify(v)) == v)
}

test("parse: map unicode") {
val input = "[{\"\\u\":\"\\u\"}]"
val output = JSON.parse(input).asInstanceOf[List[Map[String, String]]](0)("\\u")
assert(output == "\\u")
}

test("parse: map unicode2") {
val input = "[{\"\\uD835\":\"\\uD835\"}]"
val output = JSON.parse(input).asInstanceOf[List[Map[String, String]]](0)("\uD835")
assert(output == "\uD835")
}

test("parse: map unicode3") {
//actual string is 𝐇𝐨𝐧𝐝𝐚
val input = "[{\"\\uD835\\uDC07\\uD835\\uDC28\\uD835\\uDC27\\uD835\\uDC1D\\uD835\\uDC1A\":\"\\uD835\\uDC07\\uD835\\uDC28\\uD835\\uDC27\\uD835\\uDC1D\\uD835\\uDC1A\"}]"
val output = JSON.parse(input).asInstanceOf[List[Map[String, String]]](0)("\uD835\uDC07\uD835\uDC28\uD835\uDC27\uD835\uDC1D\uD835\uDC1A")
assert(output == "\uD835\uDC07\uD835\uDC28\uD835\uDC27\uD835\uDC1D\uD835\uDC1A")
}

test("parse: map unicode4") {
val input = "[{\"シリアライゼーション\":\"シリアライゼーション\"}]"
val output = JSON.parse(input).asInstanceOf[List[Map[String, String]]](0)("シリアライゼーション")
assert(output == "シリアライゼーション")
}

test("parse: map unicode5") {
val input = "[{\"\\uDC\\uD835\\uDC07\\uDC\":\"\\uD835\\uDC07\"}]"
//𝐇
val output = JSON.parse(input).asInstanceOf[List[Map[String, String]]](0)("\\uDC\uD835\uDC07\\uDC")
assert(output == "\uD835\uDC07")
}

test("parse: true|false|null") {
List[Any](true, false, null).map(testParseSym)
assert(JSON.parse(JSON.stringify(None)) == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class TokenParseTest extends org.scalatest.FunSuite {
}

test("toTokens: single string") {
List(s"""""""", s""""hello, world"""", s""""123"""", s""""\\""""", s""""\n"""", s""""\t"""", s""""\\\\"""").map((txt) => {
List(s"""""""", s""""hello, world"""", s""""123"""", s""""\\""""",
s""""\\n"""", s""""\t"""", s""""\\\\"""", s""""\\r"""", s""""\\b"""",
s""""\\f"""", s""""\\/"""").map((txt) => {
testToToken(txt, List(JSONToken(JSONToken.STRING, txt)))
})
}
Expand Down