-
Notifications
You must be signed in to change notification settings - Fork 10
From js any #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
OlivierBlanvillain
wants to merge
6
commits into
mdedetrich:master
from
OlivierBlanvillain:from-js-any
Closed
From js any #7
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da76e15
Update testEquals with .copy()
OlivierBlanvillain 16c2537
Add JValue.fromJsAny
OlivierBlanvillain 94fc7bf
Add unsafe.JValue.fromJsAny
OlivierBlanvillain 684c252
Document and @JSExport fromJsAny
OlivierBlanvillain 5954f49
Merge branch 'master' into from-js-any
OlivierBlanvillain 01d55eb
Rework fromJsAny to return an `Option[JValue]`
OlivierBlanvillain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -31,6 +31,34 @@ sealed abstract class JValue extends Product with Serializable { | |
def toJsAny: js.Any | ||
} | ||
|
||
@JSExport | ||
object JValue { | ||
/** | ||
* Converts a Javascript object/value coming from Javascript to a [[JValue]]. | ||
* | ||
* @return | ||
*/ | ||
@JSExport | ||
def fromJsAny(json: js.Any): JValue = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this have a return type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense 👍 |
||
json match { | ||
case v if v == null => JNull | ||
case v if v.isInstanceOf[Boolean] => | ||
if (v.asInstanceOf[Boolean]) JTrue else JFalse | ||
|
||
case v if (v: Any).isInstanceOf[String] => | ||
JString(v.asInstanceOf[String]) | ||
|
||
case v: js.Array[js.Any @unchecked] => | ||
JArray(v.map(fromJsAny).toVector) | ||
|
||
case v if js.typeOf(v) == "object" => | ||
JObject(v.asInstanceOf[js.Dictionary[js.Any]].mapValues(fromJsAny).toMap) | ||
|
||
case v if js.typeOf(v) == "number" => | ||
JNumber(v.toString) | ||
} | ||
} | ||
|
||
/** Represents a JSON null value | ||
* | ||
* @author Matthew de Detrich | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
package specs.unsafe | ||
|
||
import org.scalacheck.Prop._ | ||
import utest._ | ||
import Generators._ | ||
import specs.UTestScalaCheck | ||
|
||
object JValue extends TestSuite with UTestScalaCheck { | ||
|
||
val tests = TestSuite { | ||
"The JString value should" - { | ||
"have a bijection with js.Any" - testBijection | ||
} | ||
} | ||
|
||
def testBijection = { | ||
forAll { jValue: scala.json.ast.unsafe.JValue => | ||
scala.json.ast.unsafe.JValue.fromJsAny(jValue.toJsAny) == jValue | ||
}.checkUTest() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you say Scala.js Javascript object/value rather than just Javascript object/value. This is to clear confusion from Scala.js Javascript versus Javascript alone (which the AST also supports).
Also applies to anywhere else where it happens to be mentioned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I revered the doc for toJsAny, but I'm not sure I understand the nuance here, isn't it the case that
js.Any
correspond to both Javascript values and Scala.js Javascript values?