XML Serialize anonymous class - xml-serialization

The JSON script serializer (from mvc) can serialize an anonymous class to script.
Are there some standard framework (or mvc) classes that can transform an anonymous class to xml?
Currently anonymous classes are not marked as serializable and cannot be converted to xml.
Is there a way to do this without writing reams of code?

You need reams of code.
But others have already done it for you.
Here's a jQuery plugin (client-side):
http://plugins.jquery.com/project/json2xml
Here's some C# code:
http://www.phdcc.com/xml2json.htm

I think that the above did not answer the question directly. Jim did not ask about JSON to XML serializer, but C# anonymous type to XML serializer. This class does not exist out of a box, but can be written easily. Look here:
Can I serialize Anonymous Types as xml?

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.

Convering values stored within another language to case classes

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.

Create class attributes dynamically in Scala

Is it possible to create a class (or add attributes to a class) dynamically, e.g. load field names and types from external file in Scala?
this is follow-up on Representing nested structures in scala
It is possible to achieve this with macros, and there are two techniques for that with a different set of trade-offs. Refer to our joint talk with Travis Brown for more information and a link to an example implementation: https://github.com/travisbrown/type-provider-examples/blob/master/docs/scalar-2014-slides.pdf?raw=true.
You can declare and compile a Scala class from a data structure description. It requires that you a) construct a syntactically correct class description and save it to a file (or equivalent) and then b) compile that class description to object code (i.e., a .class file). You can then load the class and use it.
This is not for the faint of heart. You need to understand the process of translation, compilation, class-loading and dynamic class binding. Even more important, you have to answer how you would actually use this in a program.
An example of dynamic class creation occurs in the Scala Play framework, where a presentation template files are translated into Scala and compiled into class files that can then be referenced from other Scala source code.

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).

scala: analogy to metaclasses in python?

in scala i need to implement something similar to python metaclasses. in my case the goal of using the metaclasses is usually to create a registry of all the subclasses of a particular base class - that is, a mapping from say a string representation of the class to a reference to the class. in python it's very convenient to put a metaclass on the base class so that nothing special needs to be done on every subclass. i'm looking to do something similar in scala. is there any way to emulate metaclasses, or otherwise do this a different way? thanks!
If you know the fully qualified name of the class, you can load it using the usual Java reflection methods in java.lang.Class, namely Class.forName(String fqClassName). Given the resulting instance of Class, instantiation is easy only if there's a zero-argument constructor, otherwise you get entangled in the messy world of all the Java reflection types.
If you want a kind of "discovery" where classes unknown at compile time and whose names are not supplied as an input or parameter of the program in some way, then the classloader approach is probably the only answer.
There's nothing similar to python's metaclasses. The registry you speak of might be possible using custom class loaders or reflection.