Can we use Codable instead of JSonSerialization to serialize? what are the advantage and disadvantages of codable compared to jsonserializer - nsjsonserialization

Can we use Codable instead of JSonSerialization to serialize?
what are the advantage and disadvantages of codable compared to jsonserialization

Related

Should I write toJson or fromJson method in Kotlin data class?

It's mentioned that a Kotlin data class is for storing data. It's equivalent to the data structure. As such, it shouldn't have any behaviour method. In clean code, implementing behaviour method in a data class would violate many principles namely, SRP, OCP, ...
I was wondering if the fromJson and toJson methods are considered as behaviour method. If it is, then where should I implement those methods in my code?
I'm using Kotlin data class to store User, Product, etc. In my supposedly clean architecture and clean code that I am learning, I restricted those data class to a corner for storing only domain data.
I tried to find information about this but I can't seem to find anyone talking about data class and these methods.
Here's the code I've imagined. It should be something like this.
data class User(val id: String, val name: String) {
fun toJson(): Json {}
fun fromJson(json: Json): User {}
}
Or should I have other class responsible for toJson and fromJson implementation?
As you have tagged your query with clean architecture I assume you refer to https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html.
From this perspective a "domain entity" should be independent from any kind of serialization format, persistence aspects or other specific "technology" or "framework".
A common pattern to handle "persistence ignorance" for domain objects is the repository pattern. An important aspect here to keep in mind in context of clean architecture is that the interface of the repository is defined in the "use case layer" (business logic) while the implementation is in the "gateway layer" or even "framework layer".

Jackson BeanSerializerModifier.modifySerializer & interfaces that modify how a JsonSerializer is handled

Some interfaces, like ResolvableSerializer & ContextualSerializer, modify how Jackson handles a JsonSerializer.
Are there any other such interfaces?
Does Jackson ever modify its behavior for subclasses of JsonSerializer, like StdSerializer, BeanSerializerBase, or BeanSerializer? (other than via standard polymorphism, of course; I'm talking about things like instanceof, or Class.isAssignableFrom(), etc.)
In an implementation of modifySerializer in a subclass of BeanSerializerModifier that I wrote, I return a subclass of JsonSerializer that wraps the argument JsonSerializer. This wrapper overrides only two methods:
serialize: only if certain conditions are met does it call serialize on the wrapped serializer
getDelegatee: returns the wrapped serializer
Questions:
should my wrapper extend some subclass of JsonSerializer instead of just JsonSerializer?
if so, should the subclass depend on the class of the wrapped serializer?
should my wrapper overload any other methods?
should my wrapper implement the same serializer modifier interfaces as the wrapped serializer? If so, then there are two problems:
I have to know every modifier interface, and update my BeanSerializerModifier to handle any new ones that are added to Jackson
I need to have a different wrapper class for each combination of modifier interfaces, which is very cumbersome
Good questions. Here are some thoughts:
Usually you should extend StdSerializer instead of "raw" JsonSerializer.
If serialization is as JSON Scalar value, you may want to extend StdScalarSerializer
Base type does not need to match, in general, although if delegating to Collection or Map serializers you may want to do that -- however, in general, you should need matching. It would get impractical soon as you correctly note.
On overloading: there are a few methods you may choose to overload, and usually just delegate to delegatee:
For polymorphic handling, define "serializeWithType(...)"
isEmpty(), if there are non-null values that correlate with concept of "empty": for example, String "" is considered empty.
acceptJsonFormatVisitor() is necessary to support JSON Schema generation and other type introspection (like generating Avro, CSV and Protobuf schemas, using matching data format modules)

Are there such thing as class methods in Scala?

Many oo languages have some facility for class methods and class variables. Ruby for example allows this with metaclasses. Is it fair to look at companion objects in a similar fashion in Scala?
Is it fair to looks at companion objects in a similar fashion in
Scala?
Yes, that's a reasonable way to think about them.
On JVM classes are not objects in the same sense as in Ruby or other dynamic languages. Sure, you can get representation of a class as a special object of class Class[_], but it is used primarily for reflection and cannot have custom methods, for example. However, JVM has notion of static methods and fields, which are associated with a class and does not require objects of the class to be used:
public class SomeClass
public static int add(int x, int y) { return x + y; }
}
public class OtherClass {
public static void main() {
int z = SomeClass.add(1, 2);
}
}
These methods and fields are essentially "global" (unless we're considering class loaders mechanism), and methods, for example, cannot be overridden in subclasses.
Scala does not let you define and use statics directly - there is no notion of "static" in the language. However, in Scala you can define singleton objects which can be used to emulate static fields and methods and at the same time are much more powerful than statics, for example, they can participate in inheritance. Internally these objects are instances of special class generated by Scala compiler for you.
Also Scala has special kind of these singleton objects, called companion objects, which share the name with some class:
class SomeClass
object SomeClass
Internally these objects are instances of completely different class than SomeClass (it can be accessed as SomeClass.type), but Scala treats them in special way, for example, you can access SomeClass private members from SomeClass companion object and vice versa. Also companion objects provide additional implicit scope. However, SomeClass.type and SomeClass are completely different classes.
In short, yes, you can think of companion objects as containers for "class" methods and fields, but only to some extent. Companion objects are not metaclasses in any way; they have completely different semantics and implementation.

Jackson customize Map ValueInstantiator

By default Jackson creates instances of java.util.LinkedHashMap when deserializing any Map types. How can this be customized to have Jackson create a different Map implementation, but still continue to use its default Map deserializer?
There are multiple ways to do this, but probably the easiest is just to specify what class should be used as concrete implementation of an abstract class. So something like:
SimpleModule myModule = new SimpleModule(...);
myModule.addAbstractTypeMapping(Map.class, SomeOtherMap.class);
objectMapper.registerModule(myModule);
this works for both defaulting of common abstract types (List, Collection, Map) as well as for specifying concrete implementations of custom interfaces and abstract classes.

Override == (equality) operator in NHibernate?

With NHibernate entities, you are meant to override Equals and GetHashCode. Is it a good idea to override the == operator to use the .Equals implementation also?
Yes, it is a more general .NET "best practice" to keep Equals(), operator== and GethasCode() consistent.
See Guidelines for Overloading Equals() and Operator == .
Why do you believe you need to override Equals and GetHashCode when using NHibernate? NHibernate guarantees entity equality for any entity accessed in the same ISession. See Considering object identity in the documentation.
Edited to add:
After re-reading the question and doing some googling, I have to admit I had no idea that the equality operator (==) could be overridden in C#.