Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add crossbuild for scala native, update scala-js, update docs
- Loading branch information
1 parent
034fdc8
commit b45b2dc
Showing
10 changed files
with
352 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
Scala-json is licensed under the conditions in the LICENSE document. The project includes | ||
dependencies to other third-party projects that are obtained through SBT dependency management. | ||
dependencies to other third-party projects that are obtained through SBT or other dependency management. | ||
|
||
These projects are licensed as follows: | ||
|
||
scala - (http://www.scala-lang.org/license.html) | ||
scala-js - (https://github.com/scala-js/scala-js/blob/master/LICENSE) | ||
scala-native - (https://github.com/scala-native/scala-native/blob/master/LICENSE) | ||
jackson - (http://wiki.fasterxml.com/JacksonLicensing) | ||
jansson - (https://github.com/akheron/jansson/blob/master/LICENSE) | ||
tut - (https://github.com/tpolecat/tut/blob/master/LICENSE) | ||
scalamacros.org paradise "macro paradise" - (https://github.com/scalamacros/paradise/blob/2.11.7/LICENSE.md) | ||
utest - unknown license (https://github.com/lihaoyi/utest) |
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
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
124 changes: 124 additions & 0 deletions
124
native/src/main/scala/json/internal/JanssonDeserializer.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,124 @@ | ||
/* | ||
* Copyright 2017 MediaMath, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package json.internal | ||
|
||
import scalanative.native | ||
|
||
import janssonConstants._ | ||
import jansson._ | ||
import native._ | ||
import json._ | ||
|
||
object JanssonDeserializer { | ||
def throwError(error: Ptr[json_error_t]): Unit = { | ||
val line = !error._1 | ||
val column = !error._2 | ||
val position = !error._3 | ||
val source = native fromCString error._4.cast[CString] | ||
val text = native fromCString error._5.cast[CString] | ||
|
||
val errString = s"$text. line: $line col: $column pos: $position: $source" | ||
|
||
sys.error(errString) | ||
} | ||
|
||
def parseString(jsonString: String): JValue = { | ||
val error = stackalloc[json_error_t] | ||
|
||
val decoded = json_loads(native toCString jsonString, JSON_DECODE_ANY, error) | ||
|
||
if(decoded == null) | ||
throwError(error) | ||
|
||
val output = nativeToJSON(decoded) | ||
|
||
json_delete(decoded) | ||
|
||
output | ||
} | ||
|
||
def serializeString(str: String): String = { | ||
val encoded = json_string(native toCString str) | ||
|
||
val outNative = json_dumps(encoded, JSON_ENCODE_ANY) | ||
|
||
json_delete(encoded) | ||
|
||
require(outNative != null) | ||
|
||
val out = native fromCString outNative | ||
|
||
native.stdlib.free(outNative) | ||
|
||
out | ||
} | ||
|
||
def nativeToJSON(x: Ptr[json_t]): JValue = { | ||
val typ: json_type = !x._1 | ||
|
||
typ match { | ||
case JSON_OBJECT => | ||
//TODO: is this messing with ordering? | ||
JObject.empty ++ ObjectIterator(x) | ||
case JSON_ARRAY => | ||
JArray.empty ++ ArrayIterator(x) | ||
case JSON_STRING => | ||
JString(native fromCString json_string_value(x)) | ||
case JSON_INTEGER => | ||
JNumber(json_number_value(x)) | ||
case JSON_REAL => | ||
JNumber(json_number_value(x)) | ||
case JSON_TRUE => | ||
JTrue | ||
case JSON_FALSE => | ||
JFalse | ||
case JSON_NULL => | ||
JNull | ||
} | ||
} | ||
|
||
case class ObjectIterator(obj: Ptr[json_t]) extends Iterator[(String, JValue)] { | ||
private var itr = json_object_iter(obj) | ||
|
||
def hasNext: Boolean = itr != null | ||
|
||
def next(): (String, JValue) = { | ||
val key: CString = json_object_iter_key(itr) | ||
val value: Ptr[json_t] = json_object_iter_value(itr) | ||
|
||
itr = json_object_iter_next(obj, itr) | ||
|
||
(native fromCString key, nativeToJSON(value)) | ||
} | ||
} | ||
|
||
case class ArrayIterator(arr: Ptr[json_t]) extends Iterator[JValue] { | ||
private var idx = 0 | ||
|
||
override val size = json_array_size(arr).toInt | ||
|
||
def hasNext: Boolean = idx < size | ||
|
||
def next(): JValue = { | ||
val res = nativeToJSON(json_array_get(arr, idx)) | ||
|
||
idx += 1 | ||
|
||
res | ||
} | ||
} | ||
} |
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,101 @@ | ||
/* | ||
* Copyright 2017 MediaMath, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package json.internal | ||
|
||
import scala.scalanative.native | ||
import native._ | ||
import Nat._ | ||
|
||
@native.link("jansson") | ||
@native.extern | ||
object jansson { | ||
type JSON_ERROR_TEXT_LENGTH = Digit[_1, Digit[_6, _0]] | ||
type JSON_ERROR_SOURCE_LENGTH = Digit[_8, _0] | ||
|
||
type json_type = CInt //enum | ||
|
||
type json_error_t = CStruct5[ | ||
CInt, //line | ||
CInt, //column | ||
CInt, //position, | ||
CArray[CChar, JSON_ERROR_SOURCE_LENGTH], //source | ||
CArray[CChar, JSON_ERROR_TEXT_LENGTH] //text | ||
] | ||
|
||
type json_t = CStruct2[ | ||
json_type, //type | ||
CSize //refcount | ||
] | ||
|
||
type json_iter = Ptr[Byte] | ||
|
||
def json_loads(input: CString, flags: CSize, error: Ptr[json_error_t]): Ptr[json_t] = extern | ||
|
||
def json_dumps(json: Ptr[json_t], flags: CSize): CString = extern | ||
|
||
def json_delete(json: Ptr[json_t]): Unit = extern | ||
|
||
def json_array_size(json: Ptr[json_t]): CSize = extern | ||
|
||
def json_array_get(json: Ptr[json_t], index: CSize): Ptr[json_t] = extern | ||
|
||
def json_object_iter(obj: Ptr[json_t]): json_iter = extern | ||
|
||
def json_object_iter_at(obj: Ptr[json_t], key: CString): json_iter = extern | ||
|
||
def json_object_iter_next(obj: Ptr[json_t], itr: json_iter): json_iter = extern | ||
|
||
def json_object_iter_key(itr: json_iter): CString = extern | ||
|
||
def json_object_iter_value(itr: json_iter): Ptr[json_t] = extern | ||
|
||
def json_number_value(num: Ptr[json_t]): Double = extern | ||
|
||
def json_string(value: CString): Ptr[json_t] = extern | ||
|
||
def json_string_value(str: Ptr[json_t]): CString = extern | ||
} | ||
|
||
object janssonConstants { | ||
//json_type | ||
val JSON_OBJECT = 0 | ||
val JSON_ARRAY = 1 | ||
val JSON_STRING = 2 | ||
val JSON_INTEGER = 3 | ||
val JSON_REAL = 4 | ||
val JSON_TRUE = 5 | ||
val JSON_FALSE = 6 | ||
val JSON_NULL = 7 | ||
|
||
//flags | ||
val JSON_REJECT_DUPLICATES = 0x1 | ||
val JSON_DISABLE_EOF_CHECK = 0x2 | ||
val JSON_DECODE_ANY = 0x4 | ||
val JSON_DECODE_INT_AS_REAL = 0x8 | ||
val JSON_ALLOW_NUL = 0x10 | ||
|
||
val JSON_MAX_INDENT = 0x1F | ||
def JSON_INDENT(n: Int) = ((n) & JSON_MAX_INDENT) | ||
val JSON_COMPACT = 0x20 | ||
val JSON_ENSURE_ASCII = 0x40 | ||
val JSON_SORT_KEYS = 0x80 | ||
val JSON_PRESERVE_ORDER = 0x100 | ||
val JSON_ENCODE_ANY = 0x200 | ||
val JSON_ESCAPE_SLASH = 0x400 | ||
def JSON_REAL_PRECISION(n: Int) = (((n) & 0x1F) << 11) | ||
val JSON_EMBED = 0x10000 | ||
} |
Oops, something went wrong.