Convering values stored within another language to case classes - scala

Rookie question here.
I have 2 apps that utilize Hazelcast, one is in Typescript and the other is in Scala.
The Typescript app stores all the data and the Scala one interacts with it.
I need an easy way to parse items inside of a map to a case class, this is easy if the HazelCast data is saved within Scala because it can be cast but when I attempt to do this with data stored from Typescript I get the following
java.lang.ClassCastException: com.hazelcast.core.HazelcastJsonValue cannot be cast to TheCaseClass
I'm using circe and finch in the rest of the application, not sure if circe can be used here to parse it.
tl;dr Is there an easy way to convert HazelCast data stored in Typescript to a Scala case class.
Thanks

You cannot just take one arbitrary class and cast it to another class. You have to parse them. If you use Hazelcast then probably Hanzelcast Scala is what you should use.
Wiki suggest that you would have to do at least:
import com.hazelcast.Scala._
serialization.Defaults.register(conf.getSerializationConfig)
though it might require you to write your own custom serialization.

Related

best approach to parse JSON using generic types in flutter/dart

In my project, I have multiple model classes consisting of their own JSON parsing methods, how can I use generic type to increase code reusability.
NOTE: for HTTP request am using DIO package
The generic type you'd be referencing would have to capture the same specifics as the portion of the classes that are performing JSON parsing and serializing in your existing classes. You're just introducing a complicated interaction.
It's very hard to get past the fundamental nature of Dart, in that the member variable access cannot be somehow performed with the string names of those members. This isn't JavaScript or Perl or Ruby. :) So compile-time builder systems like json_serializable, or edit-time processing like Dart Data Class Generator for VSCode, are essential to the process.

Generate scala source for a class instance using reflection

Assuming you have an instance of a class.
What is the best approach to generate valid scala source code, which could be written out into a file and compiled, of that instance during runtime?
(Utilizing the scala reflection-api/macros?)
Is it possible to parse the AST representation into source code?
No it's not possible. Class file contains JVM byte-code that has nothing to do with Scala. You can try use Java-decompiler (http://varaneckas.com/jad/ for example), but you wouldn't be able to get something readable.
As I unserstand scala is moving towards new platform (Dotty), and maybe in future it will be possible.

Is there any suitable way to generate a [jpg, png etc.] syntax diagram(and/or AST) directly from Scala Parser Combinators?

The only ways I am aware of, aren't "direct":
converting to ANTLR format and using its own visualizer
VISUALLANGLAB, which it seems to require an entire mouse-clicks "rewrite"
implementing a converter by myself (which would be funny, but time-consuming)
second link below
Related:
comparison
wrapper
a 3rd party attempt
The second link suggests to debug adding an implicitly method to the parsers:
implicit def toLogged(name:String) = new {
def !!![T](p:Parser[T]) = log(p)(name)
}
May be an AST would be more feasible/usefull; but the question remains similar.
I might have misunderstood your question.
Scala parser combinators are used to parse strings to instances of types that you can use (either custom or built-in). The result is a structure of Scala instances that you decide, this could be anything.
You could create a parser that parses your arbitrary string into instances of a well known java structure for example ECore.
Without a usecase it's hard to suggest the best road for your problem. Maybe Xtext can help you: http://www.eclipse.org/Xtext/. Xtext has quite a few built-in features, however it's an Eclipse plugin and you might need something else.

Mapping Java beans and Scala case classes to MongoDB objects

I am currently struggling with this issue here..
In our system, we use Java beans and Scala case classes, and they often contains one another.
So, i am looking for a good solution for how to map these objects to mongoObjects, so i can save/load them from the database.
For this, i tried Morphia, but unfortunately it won't serialize back from json to object because it cannot construct a case class.
So I tried Salat, but this one only works with case classes and not java beans :(
Do anyone have an idea about how to do this?
Salat developer here.
If you want to use Salat, you could convert your Java beans to Scala case classes and annotate the constructor params with #scala.reflect.BeanProperty for interoperability with Java (if this is really necessary).

Easy Scala Serialization?

I'd like to serialization in Scala -- I've seen the likes of sjson and the #serializable annotation -- however, I have been unable to see how to get them to deal with 1 major hurdle -- Type Erasure and Generics in Libraries.
Take for example the Graph for Scala Library. I make heavy use of it in my code and would like to write several objects holding graphs to disk throughout my code for later analysis. However, many times the node and edge types are encapsulated in generic type arguments of another class I have. How can I properly serialize these classes without either modifying the library itself to deal with reflection or "dirtying" my code by importing a large number of Type Classes (serialization according to how an object is being viewed is wholly unsatisfying anyways...)?
Example,
class Container[N](val g: Graph[N,DiEdge]) {
...
}
// in another file
def myMethod[N](container: Container[N]): Unit = {
<serialize container somehow here>
}
To report on my findings, Java's XStream does a phenomenal job -- anything and everything, generics or otherwise, can be automatically serialized without any additional input. If you need a quick and no-work way to get serialization going, XStream is it!
However, it should be noted that the output XML will not be particularly concise without your own input. For example, every memory block used by Scala's HashMap will be recorded, even if most of them don't contain anything!
If you are using Graphs for Scala and if JSON is your serialization format, you can directly use graph-json.
Here is the code and the doc.