|
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 | + */ |
2 | 16 |
|
3 |
| -import json.{ JSJValue, JValue } |
| 17 | +package json.shadow |
4 | 18 |
|
| 19 | +import json.internal.BaseVMContext |
| 20 | +import json.{JSONAccessorProducer, JSJValue, JValue} |
| 21 | +import scalajs.js.{JSON => NativeJSON} |
| 22 | +import scala.scalajs.js.annotation.JSExport |
5 | 23 | import scalajs.js
|
6 | 24 |
|
7 |
| -object VMContext { |
| 25 | +object VMContext extends BaseVMContext { |
8 | 26 | 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) |
16 | 30 |
|
17 | 31 | //run it again incase the reviver didnt work
|
18 |
| - JSJValue.from(parsed) |
| 32 | + JSJValue from parsed |
19 | 33 | }
|
20 | 34 |
|
21 | 35 | def fromAny(value: Any): JValue = JSJValue.from(value)
|
22 | 36 |
|
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)) |
24 | 53 |
|
25 |
| - trait JValueBase |
26 | 54 | }
|
0 commit comments