Skip to content

Commit 5051687

Browse files
committed
Merge branch 'develop'
2 parents eb7e432 + 707ad72 commit 5051687

26 files changed

+511
-121
lines changed

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2015 MediaMath, Inc
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

js/src/main/scala/json/JSImplicits.scala

Lines changed: 0 additions & 13 deletions
This file was deleted.

js/src/main/scala/json/JSJValue.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package json
218

319
import scala.scalajs.js
4-
20+
import scalajs.js.{JSON => NativeJSON}
521
import js.JSConverters._
622

723
object JSJValue {
Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
1-
package json.shadow
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

3-
import json.{ JSJValue, JValue }
17+
package json.shadow
418

19+
import json.internal.BaseVMContext
20+
import json.{JSONAccessorProducer, JSJValue, JValue}
21+
import scalajs.js.{JSON => NativeJSON}
22+
import scala.scalajs.js.annotation.JSExport
523
import scalajs.js
624

7-
object VMContext {
25+
object VMContext extends BaseVMContext {
826
def fromString(str: String): JValue = {
9-
//TODO: use parse 2nd arg for inline wrapper!
10-
11-
def reviver(key: js.Dynamic, value: js.Dynamic): JValue =
12-
JSJValue from value
13-
14-
val parsed = js.Dynamic.global.JSON.parse(str,
15-
(key: js.Dynamic, value: js.Dynamic) => JSJValue from value)
27+
def reviver = (key: js.Any, value: js.Any) =>
28+
(JSJValue from value).asInstanceOf[js.Any]
29+
val parsed = NativeJSON.parse(str, reviver)
1630

1731
//run it again incase the reviver didnt work
18-
JSJValue.from(parsed)
32+
JSJValue from parsed
1933
}
2034

2135
def fromAny(value: Any): JValue = JSJValue.from(value)
2236

23-
trait JValueCompanionBase
37+
trait JValueCompanionBase {
38+
implicit case object JsAnyAccessor extends JSONAccessorProducer[js.Any, JValue] {
39+
val clazz = classOf[JValue]
40+
41+
def createJSON(obj: js.Any): JValue = JValue from obj
42+
def fromJSON(jValue: JValue): js.Any = JSJValue toJS jValue
43+
}
44+
}
45+
46+
trait JValueBase { _: JValue =>
47+
//this adds JSON.stringify support
48+
@JSExport def toJSON: js.Any = JSJValue toJS this
49+
}
50+
51+
def quoteJSONString(string: String): StringBuilder =
52+
new StringBuilder(NativeJSON.stringify(string))
2453

25-
trait JValueBase
2654
}

js/src/test/scala/json/ExportTest.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package json
218

319
import scala.scalajs.js.annotation.JSExport
420
import scalajs.js
521

22+
import utest._
623
import utest.ExecutionContext.RunNow
724

825
class ExportTest extends js.JSApp {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package json
18+
19+
import utest._
20+
import scalajs.js
21+
import scalajs.js.{JSON => NativeJSON}
22+
23+
object JSTest extends TestSuite {
24+
val tests = TestSuite("test JS" - {
25+
"test json" - {
26+
import Sample._
27+
28+
val foo = Foo("a", 1)
29+
val fw = FooWrapper("b", Set(foo))
30+
val fooJs = fw.js
31+
32+
val nativeSer = NativeJSON.stringify(fooJs.toJSON)
33+
val reserd = JValue fromString nativeSer
34+
35+
require(reserd == fooJs)
36+
}
37+
})
38+
}

jvm/src/main/scala/main/json/internal/JValueObjectDeserializer.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package json.internal
218

319
import com.fasterxml.jackson.core.{ JsonParser, JsonToken }
@@ -7,7 +23,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule
723
import com.fasterxml.jackson.databind.{ DeserializationContext, ObjectMapper }
824
import json._
925

10-
import scala.collection.immutable.VectorBuilder
26+
import scala.collection.immutable.{StringOps, VectorBuilder}
1127

1228
class JValueObjectDeserializer extends StdDeserializer[JValue](classOf[JValue]) {
1329
import com.fasterxml.jackson.core.JsonToken._
@@ -83,7 +99,6 @@ class JValueObjectDeserializer extends StdDeserializer[JValue](classOf[JValue])
8399
x
84100
}
85101

86-
//super.deserializeWithType()
87102
def parseObject(jp: JsonParser, ctx: DeserializationContext): JObject = {
88103
require(jp.getCurrentToken == START_OBJECT, "not start of object")
89104

jvm/src/main/scala/main/json/shadow/VMContext.scala

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package json.shadow
218

319
import json._
4-
import json.internal.JValueObjectDeserializer
20+
import json.internal.{BaseVMContext, JValueObjectDeserializer}
21+
22+
import scala.collection.immutable.StringOps
23+
24+
object VMContext extends BaseVMContext {
25+
trait JValueCompanionBase
26+
27+
trait JValueBase
528

6-
object VMContext {
729
val localMapper = new ThreadLocal[JValueObjectDeserializer] {
830
override protected def initialValue: JValueObjectDeserializer =
931
new JValueObjectDeserializer
@@ -21,8 +43,42 @@ object VMContext {
2143

2244
def fromAny(value: Any): JValue = JValue.fromAnyInternal(value)
2345

24-
trait JValueCompanionBase
46+
//modified some escaping for '/'
47+
final def quoteJSONString(string: String): StringBuilder = {
48+
require(string != null)
2549

26-
trait JValueBase
50+
val len = string.length
51+
val sb = new StringBuilder(len + 4)
52+
53+
sb.append('"')
54+
for (i <- 0 until len) {
55+
string.charAt(i) match {
56+
case c if c == '"' || c == '\\' => //Set('"', '\\') contains c =>
57+
sb.append('\\')
58+
sb.append(c)
59+
//not needed?
60+
/*case c if c == '/' =>
61+
// if (b == '<') {
62+
sb.append('\\')
63+
// }
64+
sb.append(c)*/
65+
case '\b' => sb.append("\\b")
66+
case '\t' => sb.append("\\t")
67+
case '\n' => sb.append("\\n")
68+
case '\f' => sb.append("\\f")
69+
case '\r' => sb.append("\\r")
70+
case c =>
71+
if (c < ' ') {
72+
val t = "000" + Integer.toHexString(c)
73+
sb.append("\\u" + t.substring(t.length() - 4))
74+
} else {
75+
sb.append(c)
76+
}
77+
}
78+
}
79+
sb.append('"')
80+
81+
sb
82+
}
2783
}
2884

project/ScalaJSON.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
import sbt.Keys._
218
import sbt._
319
import org.scalajs.sbtplugin.ScalaJSPlugin
@@ -27,7 +43,8 @@ object ScalaJSON {
2743
scalaVersion := "2.11.5",
2844
organization := "com.mediamath",
2945
organizationName := "MediaMath, Inc",
30-
organizationHomepage := Some(url("http://www.mediamath.com"))
46+
organizationHomepage := Some(url("http://www.mediamath.com")),
47+
crossScalaVersions := Seq(scalaVersion.value, "2.10.4")
3148
)
3249

3350
val commonSettings = baseSettings ++ Seq(
@@ -61,7 +78,6 @@ object ScalaJSON {
6178
def settings(jsonJS: Project, jsonJVM: Project) = baseSettings ++ tut.Plugin.tutSettings ++ Seq(
6279
publish := {},
6380
publishLocal := {},
64-
crossScalaVersions in ThisBuild := Seq(scalaVersion.value, "2.10.4"),
6581

6682
genDocsTask <<= (tut.Plugin.tut, baseDirectory, version) map { (outFiles, baseDir, ver) =>
6783
for((outFile, _) <- outFiles) {

shared/src/main/scala/json/JArray.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2015 MediaMath, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package json
218

319
import scala.collection.generic.{ CanBuildFrom, GenericCompanion }
@@ -105,7 +121,7 @@ final case class JArray(override val values: IndexedSeq[JValue]) extends JValue
105121
values foreach { v =>
106122
if (!isFirst) out.append("," + settings.spaceString)
107123

108-
out append v.toJSONStringBuilder(settings)
124+
out append v.toJSONStringBuilder(settings, lvl + 1)
109125

110126
isFirst = false
111127
}

0 commit comments

Comments
 (0)