JRuby : How to specify java primitive array - jruby

How can I specify Java primitive array?
Java Object array type can be specified like
['a'].to_java(:string).is_a? java.lang.String[]
-> true
but I don't know how to pass Java primitive array class.
[1].to_java(:byte).is_a? ???
[1].to_java(:byte).is_a? byte[]
NameError: undefined local variable or method `byte' for main:Object
[1].to_java(:byte).is_a? :'byte[]'
TypeError: class or module required
[1].to_java(:byte).is_a? java.lang.byte[]
ArgumentError: illegal package name component: byte
What is a JRuby type of java primitive array type?

I found an answer.
[1].to_java(:byte).is_a?([].to_java(:byte).class)
-> true

Related

Kotlin + Moshi serialization of object type

I have following code in Kotlin:
sealed class ParentClass
data class ChildA(val prop: String): ParentClass()
object ChildB: ParentClass()
but when I try to serialize it into JSON with Moshi I get following Error:
Caused by: java.lang.IllegalArgumentException: Cannot serialize object declaration ChildB
Failed to serialize obj: ChildB of type: class ChildB to a map
I don't want to include full stack trace due to confidentiality, but essentially it fails on this line. I wonder if there is a way to serialize Kotlin object types into JSON and back?
https://github.com/ZacSweers/MoshiX/tree/main/moshi-sealed should support that use case, I believe.

Is there a way to JSON.stringify an object and have it's type be JSON?

If I do this:
import { MyType } from 'somewhere';
class MyClass {
myObj: MyType = new MyType();
updateObject(newVal: string): void {
myObj.thing = newVal;
this.saveStuff(JSON.stringify(myObj));
}
saveStuff(json: JSON): void {
// http request...
}
}
I get an error that I'm passing a string, not JSON. (I understand that I am in fact passing a string) How can I make it take the string as JSON?
I tried casting the string as JSON, ie: JSON.stringify(foo) as JSON or <JSON> JSON.stringify(foo). But I get a "Type 'string' cannot be converted to type 'JSON'." error both ways.
What you are doing with the TypeScript type annotation is to announce the type you expect a value to have (you don't declare the type). More often than never it happens that you set a type annotation and when you run the code you discover that the actual type is something else.
In this case though the TS compiler can evaluate the types beforehand. It knows that JSON.stringify returns a string. Since you have annotated the saveStuff method to accept a JSON object, it will give you a compiler error for the type mismatch.
Regardless of its content, a string remains a string. It may contain JSON, XML or a poem. It will still be nothing else than a string. The JSON class is just a utility class that provides you with a way to serialize and deserialize a JavaScript object into and from a string (with JSON content).
JSON is not a type. When you parse a string by calling JSON.parse(str), what you get is an object literal.
In your code, as you call JSON.stringify(foo), you are converting the object literal foo to a string.
Thus, your saveStuff() receives a string.
JSON stands for JavaScript Object Notation. It is just a specification on how to represent an object.

Why I parse json into a Java List, but not a Scala List?

I am attempting to parse a json object that contains a list. I am able to parse the list if the field is backed by a Java List, but it fails if the field is backed by a Scala list. What is the difference between parsing into a Scala List vs a Java List, and what do I have to change to be able to parse this into a Scala List?
object JsonParsingExample extends App {
val objectMapper = new ObjectMapper()
// This line succeeds.
objectMapper.readValue("""{"list": ["a","b"]}""", classOf[JavaList])
// This line fails.
objectMapper.readValue("""{"list": ["a","b"]}""", classOf[ScalaList])
}
case class JavaList() {
#JsonProperty(value = "list")
var myList: java.util.ArrayList[String] = null
}
case class ScalaList() {
#JsonProperty(value = "list")
var myList: List[String] = null
}
The error message I receive is:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of scala.collection.immutable.List, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
Jackson doesn't know anything about Scala types by default (otherwise it would have to depend on scala-library). To teach it, use jackson-module-scala.
Because the scala.collection.immutable.List is actually an abstract class. Generally when you use List("a", "b", "c") is the object List.apply() which is coming from this line: https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/immutable/List.scala#L452 and that's actually an inner class (something called scala.collection.immutable.$colon$colon).

No Constructor found when using PowerMockito for mocking JSONObject constructor

I'm using a custom class for JSONObject (madison.util.json.JSONObject) instead of the standard org.json.JSONObject and am trying to mock a constructor(String) call for JSONObject.class using PowerMockito.
PowerMockito.whenNew(JSONObject.class).withArguments(String.class).thenReturn(jsonStub);
I'm getting teh following error:
No constructor found in class 'madison.util.json.JSONObject' with parameter types: [ null ].
Can anybody advise what is the problem here?
Thanks
you pass a Class as argument not a String.
To pass a string without take care of its value, you can use:
PowerMockito.whenNew(JSONObject.class).withArguments(Matchers.anyString()).thenReturn(jsonStub);
Otherwise, If you need a String.class as argument try:
whenNew(MimeMessage.class).withParameterTypes(MyParameterType.class).withArguments(isA(MyParameter.class)).thenReturn(mimeMessageMock);
https://groups.google.com/forum/#!msg/powermock/ncH_2u39UBM/Rtk0-_FufzQJ

what is the difference between * and Object

Let's say I have a class with some a couple properties:
public class MyClass {
public var fooProp:*;
public var barProp:Object;
}
What is the difference, practically speaking, between these? Are there variable types I can later assign to fooProp that I cannot assign to barProp?
Only untyped variables can hold the value undefined. Untyped variables are variables that either lack any type annotation, or use the asterisk * symbol for type annotation.
From ActionScript data type descriptors:
In previous versions of ActionScript, a variable with no type
annotation was automatically assigned the Object data type. This is no
longer true in ActionScript 3.0, which now includes the idea of a
truly untyped variable. Variables with no type annotation are now
considered untyped. If you prefer to make it clear to readers of your
code that your intention is to leave a variable untyped, you can use
the new asterisk (*) symbol for the type annotation, which is
equivalent to omitting a type annotation. The following example shows
two equivalent statements, both of which declare an untyped variable:
var x
var x:*
Only untyped variables can hold the value undefined. If you attempt to
assign the value undefined to a variable that has a data type, Flash
Player or Adobe AIR will convert the value undefined to the default
value of that data type. For instances of the Object data type, the
default value is null, which means that Flash Player or Adobe AIR will
convert the value undefined to null if you attempt to assign undefined
to an Object instance.
As an example:
var t:* = undefined;
trace(t); // outputs: undefined
var t:Object = undefined;
trace(t); // outputs: null