Skip to content

Commit a716af4

Browse files
committed
initial commit
0 parents  commit a716af4

26 files changed

+2585
-0
lines changed

README.md

Whitespace-only changes.

build.sbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ScalaJSON.settings
2+
3+
lazy val jsonJs = Project("scala-json", file("scalajs"))
4+
.enablePlugins(ScalaJSPlugin)
5+
.settings(ScalaJSON.jsSettings: _*)
6+
7+
lazy val json = project.aggregate(jsonJs).settings(ScalaJSON.settings: _*).aggregate(jsonJs)

project/ScalaJSON.scala

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import sbt.Keys._
2+
import sbt._
3+
import org.scalajs.sbtplugin.ScalaJSPlugin
4+
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
5+
import sbt.dsl._
6+
7+
object Repository {
8+
val host = "artifactory.mediamath.com"
9+
val artifactory = s"https://$host/artifactory/"
10+
val snapshots = s"${artifactory}libs-snapshot"
11+
val releases = s"${artifactory}libs-release"
12+
val remote = s"${artifactory}remote-repos"
13+
val pluginSnapshots = s"${artifactory}sbt-plugins-snapshot-local"
14+
val pluginRelease = s"${artifactory}sbt-plugins-release-local"
15+
16+
val snapshotRepo = "snapshots" at Repository.snapshots
17+
val releaseRepo = "releases" at Repository.releases
18+
val remoteRepo = "remote" at Repository.remote
19+
20+
def repo(isSnapshot: Boolean) = if (isSnapshot) Repository.snapshots else Repository.releases
21+
def publishTo(isSnapshot: Boolean) = repo(isSnapshot) + "-local"
22+
def globalPublishTo(isSnapshot: Boolean) = repo(isSnapshot) + "-global"
23+
def userCredentials = (Path.userHome / ".ivy2" / "credentials" ** "*").filter(_.isFile).get.map(Credentials(_))
24+
}
25+
26+
object ScalaJSON {
27+
val commonSettings = Seq(
28+
version := "0.1",
29+
scalaVersion := "2.11.5",
30+
organization := "com.mediamath",
31+
organizationName := "MediaMath, Inc",
32+
organizationHomepage := Some(url("http://www.mediamath.com")),
33+
credentials ++= Repository.userCredentials,
34+
publishMavenStyle := true,
35+
crossPaths := true,
36+
publishTo := Some("publish" at Repository.globalPublishTo(isSnapshot.value)),
37+
publishArtifact in Test := false,
38+
39+
scalacOptions ++= Seq("-deprecation", "-language:_", "-unchecked", "-Xlint",
40+
"-Xlog-free-terms", "-target:jvm-1.7", "-encoding", "UTF-8"),
41+
42+
libraryDependencies <++= scalaVersion { x =>
43+
Seq(
44+
"org.scala-lang" % "scala-reflect" % x,
45+
"org.scala-lang" % "scala-compiler" % x
46+
)
47+
},
48+
49+
libraryDependencies ++= Seq(
50+
"com.lihaoyi" %%% "utest" % "0.3.0" % "test",
51+
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.4.1"
52+
),
53+
54+
testFrameworks += new TestFramework("utest.runner.Framework")
55+
)
56+
57+
val settings = commonSettings ++ Seq(
58+
name := "scala-json"
59+
)
60+
61+
val jsSettings = commonSettings ++ Seq(
62+
name := "scala-json",
63+
64+
unmanagedSourceDirectories in Compile +=
65+
(baseDirectory in ThisBuild).value / "src" / "main" / "scala",
66+
unmanagedSourceDirectories in Test +=
67+
(baseDirectory in ThisBuild).value / "src" / "test" / "scala"
68+
)
69+
}

project/build.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sbt.version=0.13.7
2+

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package json
2+
3+
import scalajs.js
4+
5+
object JSImplicits {
6+
implicit class JSAnyExt(val value: js.Any) extends AnyVal {
7+
def toJValue = JSJValue from value
8+
}
9+
10+
implicit class JSValueExt(val value: JValue) extends AnyVal {
11+
def toJS = JSJValue toJS value
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package json
2+
3+
import scala.scalajs.js
4+
import json.internal.DefaultJVMShadowContext.{ VMContext => JVMContext }
5+
6+
import js.JSConverters._
7+
8+
object JSJValue {
9+
def from(v: Any): JValue = v match {
10+
case x: JValue => x
11+
case seq0: js.Array[Any] =>
12+
val seq: Seq[Any] = seq0
13+
val jvals: Seq[JValue] = seq.map(JSJValue.from)
14+
15+
JArray(jvals)
16+
case x0: js.Object =>
17+
val x = x0.asInstanceOf[js.Dynamic]
18+
val seq = (js.Object keys x0).toSeq.map { key =>
19+
val value = JSJValue from x.selectDynamic(key)
20+
JString(key) -> value
21+
}
22+
JObject(seq: _*)
23+
case x: String => JString(x)
24+
case x => JVMContext.fromAny(x)
25+
}
26+
27+
def toJS(from: JValue): js.Any = from match {
28+
case x: JObject =>
29+
val vals = for ((JString(key), value) <- x.iterator)
30+
yield key -> toJS(value)
31+
32+
vals.toMap.toJSDictionary
33+
case JArray(values) =>
34+
values.map(toJS).toJSArray
35+
case JUndefined => js.undefined
36+
case JNull => null
37+
case x => x.value.asInstanceOf[js.Any] //assumed to be a primitive here
38+
}
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package json.shadow
2+
3+
import json.internal.DefaultJVMShadowContext
4+
import json.{ JSJValue, JValue }
5+
6+
import scalajs.js
7+
8+
object VMContext {
9+
def fromString(str: String): JValue = {
10+
//TODO: use parse 2nd arg for inline wrapper!
11+
val parsed = js.Dynamic.global.JSON.parse(str)
12+
13+
JSJValue.from(parsed)
14+
}
15+
16+
def fromAny(value: Any): JValue = JSJValue.from(value)
17+
18+
trait JValueCompanionBase extends DefaultJVMShadowContext.VMContext.JValueCompanionBase
19+
20+
trait JValueBase extends DefaultJVMShadowContext.VMContext.JValueBase
21+
}

src/main/scala/json/JArray.scala

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package json
2+
3+
import scala.collection.generic.{ CanBuildFrom, GenericCompanion }
4+
import scala.collection.mutable.Builder
5+
import scala.collection.immutable.VectorBuilder
6+
import scala.collection.{ IterableLike, immutable }
7+
import java.util.UUID
8+
9+
object JArray { //extends GenericCompanion[scala.collection.immutable.Iterable] {
10+
//def apply(values: IndexedSeq[JValue]): JArray = new JArray(values)
11+
12+
//def unapply(obj: JArray): Option[IndexedSeq[JValue]] = Some(obj.values)
13+
14+
lazy val empty = apply(IndexedSeq.empty)
15+
16+
def newCanBuildFrom = new CanBuildFrom[TraversableOnce[JValue], JValue, JArray] {
17+
def apply(from: TraversableOnce[JValue]) = newJArrayBuilder // ++= from
18+
def apply() = newJArrayBuilder
19+
}
20+
21+
implicit def canBuildFrom: CanBuildFrom[TraversableOnce[JValue], JValue, JArray] =
22+
newCanBuildFrom
23+
24+
def newJArrayBuilder: Builder[JValue, JArray] = new JArrayBuilder
25+
26+
def apply(seq: TraversableOnce[JValue]): JArray = new JArray(seq.toIndexedSeq)
27+
//def apply[T <: JValue](x: T, xN: T*): JArray = apply(x +: xN)
28+
29+
class JArrayBuilder extends Builder[JValue, JArray] {
30+
val builder = new VectorBuilder[JValue]
31+
32+
def +=(item: JValue): this.type = {
33+
builder += item
34+
this
35+
}
36+
37+
def result: JArray = {
38+
JArray(builder.result)
39+
}
40+
41+
def clear() {
42+
builder.clear
43+
}
44+
}
45+
46+
def newBuilder[A]: Builder[A, immutable.Iterable[A]] =
47+
newJArrayBuilder.asInstanceOf[Builder[A, immutable.Iterable[A]]]
48+
}
49+
50+
final case class JArray(override val values: IndexedSeq[JValue]) extends JValue with scala.collection.immutable.Iterable[JValue] with IterableLike[JValue, JArray] {
51+
lazy val uuid = UUID.randomUUID.toString
52+
53+
def toJString: JString = JString("array " + uuid) //this... should be different
54+
def toJNumber: JNumber = JNaN
55+
def toJBoolean: JBoolean = JTrue
56+
//override def toObject
57+
58+
def length = values.length
59+
60+
def iterator: Iterator[JValue] = values.iterator
61+
62+
def value = values.map(_.value)
63+
64+
//override def companion: GenericCompanion[scala.collection.immutable.Iterable] = JArray
65+
override def newBuilder = JArray.newJArrayBuilder
66+
67+
override def toSeq = values
68+
override def seq = this
69+
70+
override def jValue = this
71+
override def toJArray: JArray = this
72+
//override def keys = (0 until values.length).map(JNumber(_)).toSet
73+
74+
override def jObject: JObject = throw GenericJSONException("Expected JObject")
75+
override def jArray: JArray = this
76+
override def jNumber: JNumber = throw GenericJSONException("Expected JNumber")
77+
override def jString: JString = throw GenericJSONException("Expected JString")
78+
override def jBoolean: JBoolean = throw GenericJSONException("Expected JBoolean")
79+
80+
override def toString = toJSONString
81+
82+
def apply(key: JNumber): JValue = apply(key: JValue)
83+
84+
override def apply(key: JValue): JValue = {
85+
val jNum = key.toJNumber
86+
val jInt = jNum.num.toInt
87+
88+
if (jNum.num != jInt) JUndefined
89+
else if (jInt < 0) JUndefined
90+
else if (jInt >= values.length) JUndefined
91+
else values(jInt)
92+
}
93+
94+
def ++(that: JArray): JArray =
95+
JArray(values ++ that.values)
96+
97+
def toJSONStringBuilder(settings: JSONBuilderSettings,
98+
lvl: Int): StringBuilder = {
99+
val out = new StringBuilder
100+
101+
out.append("[")
102+
103+
var isFirst = true
104+
values foreach { v =>
105+
if (!isFirst) out.append("," + settings.spaceString)
106+
107+
out append v.toJSONStringBuilder(settings)
108+
109+
isFirst = false
110+
}
111+
112+
out.append("]")
113+
}
114+
}

0 commit comments

Comments
 (0)