if my class is not public than XML Encoder and Decoder doesn`t work - xml-serialization

Xml is work only class is public.what i do if class is not public?

If you are talking about .NET XML serialization with XmlSerializer then this type can serialize only public classes. You could use DataContractSerializer instead.

For Java, java.beans.XMLDecoder also only unmarshalls public classes. There is no way to overwrite this behavior and to provide custom object handlers.
There are other libraries, such as XStream, which have various options to encode/decode other types of classes, control the marshalling process or use factory classes to create instances of private classes which were otherwise not accessible.

Related

Kotlin inline class for #Id-annotated properties

In my business logic I have to deal with a lot of entity IDs, all of them of type String, which can cause confusion especially when you pass a couple of them as method parameters. So I thought about introducing a little type safety with inline classes. I know, inline classes are still marked as experimental in v1.3. Nevertheless, has anyone ever tried to use an inline class as the #Id property within a DB mapping context, in my case a MongoDB with Spring Data.
#Entity
class User {
#Id
var id: UserId
}
with
inline class UserId(val id: String)
I guess there is no unboxing of the underlying property, so _id will end up as an object in the DB? And what about Spring's CrudRepository interfaces? It seems compilable but will it work eventually:
interface UserRepository : CrudRepository<User, UserId>
Probably using AttributeConverter to convert the inline class to a primitive might do the job. Any experiences with this?
Inline classes result in completely new types, not just a typed Alias. Even if our code base knows what this new type is the MongoDB doesn't right? So you cannot store the inline class directly into the corresponding primitive type Fields
There is an unresolved ticket for Spring Data Commons: https://github.com/spring-projects/spring-data-commons/issues/1947

How to implement a KryoSerializer for an interface where all implementing classes are private?

I'm trying to write a class to implement KryoSerializer so that I can serialize objects for use with Spark. The issue I'm having is that while all of the classes implement a public interface, all of the implementing classes are private. Kryo doesn't seem to want to allow me to define a serializer for either a private out-of-package class or an interface.
The way this issue manifests is that when I attempt to define the KryoSerializer class, I get an error that class [implementation] in package graph cannot be accessed in package [same package].
What I'm hoping someone can help with, is a strategy for solving this issue.
I understand the reasons why Kryo wants to serialize and deserialize concrete objects. But, in this case, since I am defining my own KryoSerializer anyway, it actually would make more sense to define serialization for the interface. Is there a way to trick Kryo into doing the right thing?
(The reason this will work, is that there is a related Object that has functions to take an instance with the interface and write or read from a stream. My serializer would essentially wrap those functions while adding some serialization format version information.)
One possibility I thought of is the reflection trick. When deserializing a class with private/final members, we sometimes use reflection to make the private member accessible and writable, set the value, then set it back to private/final. I'm not sure if its possible to do that for a private class in another packager, but even if it is, it seems rather ugly and inefficient.
Another possibility would be to define new classes that extend the private classes, along with a set of implicits to convert among them. That would also be rather ugly though, for a few reasons, and there are quite a few private classes at issue.
Can anyone suggest an approach? Advise regarding pitfalls I should avoid?

Constructor and Unit Testing

I have a class XmlRecord. This class will deal with reading/writing to an xml file. At the moment, I have the following for that class:
class XmlRecord {
private val _file = new File("file.xml")
}
I want this class to somehow create the file if it doesn't exist. I know how to achieve this but I'm unsure how to design it in an Object Orientated way. I think I have two options:
Do I add a code to the constructor (or a call to a private method) that will create this file automatically if it doesn't exist. My problem with this method is that how do I unit test this as this code is effectively private code? Would I have to inject the File dependency so it could be mocked during testing?
Do I get the constructor to return an exception or implement a public method for the class so that the caller can use it to check if a file needs to be created? If so, the caller would then call another public method that would create the file. Again I think I would need to inject the dependency.
I hope that makes sense. I'm just trying to get a better grasp on designing my classes.
The presence of abstractions to accomplish DIP have other design
implications in an Object Oriented program:
All member variables in a class must be interfaces or abstracts.
All concrete class packages must connect only through interface/abstract classes packages.
No class should derive from a concrete class.
No method should override an implemented method.[5]
All variable instantiation requires the implementation of a Creational pattern as the Factory Method or the Factory pattern, or the more complex use of a Dependency Injection framework.
Dependency inversion principle

Typhoon: how to inject class instead of instance

I have a third-party library which is written in Swift. The library provides a class that has some class methods in it. Using Typhoon, I want to inject the class into one of my classes so that, under unit testing, I could inject a mock class that provides fake class methods. I'm new to Typhoon and I went though the documentation, but haven't figured out how to do it. Is this even doable with Typhoon?
Yes, in the User Guide the section on Injecting Configuration shows how to inject primitives, scalar values and so forth.
To inject a class:
[initializer injectParameterWith:[SomeClass class]];
This also applies to property injection and method injection.
To inject a selector:
[initializer injectParameterWith:NSValueFromPrimitive(#selector(selectorValue))];
Typhoon rules:
References to other definitions are resolved to the built instance.
Simple objects, primitives and scalar values are injected as-is (scalar values and primitives must be wrapped).
Collections (NSArray, NSSet, etc) that contain references to other definitions have those references resolved to the built instance. Any other values pass through as is.
There is also Typhoon Config, which allows storing configuration, simple objects and so forth in an external plist, json or properties file.

Serializing JDBC Types in GWT

I need to serialize object of uknown type(only JDBC types) in GWT. I have an object that holds list of uknown "jdbc" objects and i need it to be transfered from client to server and back. If this object is serializing to file not in gwt client environment I can hold those uknown objects in list of Object's. But GWT can't serialize objects of type Object. How can I achieve this? Any suggestions
You may be running into problems if those jdbc types are not being returned by any of your other RPC methods.
If, for instance, your class Foo isn't sent via RPC by any method other than one which returns List[Object], then GWT has no knowledge at compile time (when it generates the RPC whitelist) that Foo is a class that it should generate the code to serialize. This especially makes sense for the generated JavaScript, where avoiding code bloat from unused types is important.
You can work around this by manually adding your otherwise unreferenced classes (all possible return types from JDBC) in a dummy class that gets sent across RPC.
How do I add a type to GWT's Serialization Policy whitelist?
Alternately you can write a custom RemoteService generator to use which adds the types without a Dummy class being required.
http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html#generator
You can return them as
List<Serializable> serviceMethod();
If you guarantee that they are all Serializable. Just cast all of them to Serializable before returning from the service method.
See GWT JRE Emulation Reference, there is no Object class and GWT cannot serialize it, so you should create your own transfer object which implements IsSerializable marker interface.
Briefly, RPC can not serialize java.lang.Object. Check this links:
GWT Sending type OBJECT Via RPC and good thread here:
Serialize object and Why is GWT serialization so complicated?