How to disallow null as a value in json4s deserialization - scala

I'm deserializing data from json into case classes and just came across some malformed json providing me null for a non-optional object. I would prefer this to be parser failure, instead of setting the field value to null. That way i could safely assume that my case classes are properly populated if they parse, but I can't seem to find a way to configure the parser for this.

It is possible to initialize a field with null value and then use "#JsonInclude(Include.NON_NULL)" to ignore the field while deserialization if there wasn't any value assigned to it.
You can check for the existence of the field and interpret it as being null or not.

You could use Jackson deserializer see:
Deserialization sample
and:
Another Deserialization sample
You can also Deserialize field:
#JsonDeserialize(using = SomeDeserializer.class)
private String definition;
and than do validation in Deserializer.

Related

Mdriven Designer class attribute Allow Null=False not working

I have a class named "Project" with attribute "Name" having type of String. I have changed it from Allow Null=True to False and then I have saved the model and restarted the WECPOF prototyper in xml mode. But it still allows me to create and save instances of Project without adding a name. What might I be doing wrong?
Try to keep your attributes "nullable", i.e. Allow null = True. Why? Because you usually end up wanting to see the difference between "nothing" and "empty".
As Hans suggested, add a contraint on the class with a expression like this "not self.Name.isNullOrEmpty".
I guess the string is not null but empty "". Strings are tricky that way - the only type that is presented the same as null and as its simplest possible value.
To signal to user that you do not allow a null or empty you can do a constraint on the class or add a validation expression in the ViewModel.

Kryo Serialization empty Deserialization

I refactor my code to work with kryo serialization.
Everything works fine except deserialize a property of geomtry from certain class.
No exception is thrown (I set "spark.kryo.registrationRequired" to true).
On debug I try to collect the data and I see that the data in the geomtry is just empty. As a result I understand that the deserialize was fail.
Geomtry is from type of - Any(scala) because it is a complex property maybe.
My question is why the data is empty, And is there connection to the type 'Any' of the property.
Update :
class code: class Entity(val id:String) extends Serializable{
var index:Any = null
var geometry:Any = null
}
geometry contains centeroid, shape and coordinates(complex object)
You should not use Kryo with Scala since the behavior of many Scala classes differs from Java classes and Kryo was originally written to work with Java. You will probably encounter many weird issues like this one if you use Kryo with Scala. You should instead use chill-scala which is an extension of Kryo that handles all of Scala's special cases.

Spring batch: FieldSetMapper should set field to null instead of empty

I am using spring batch to read pipe (| delimited) separated file which have have 7 field. I created a class called MyLineMapper that extends spring's FieldSetMapper. This class maps field values provided in file to my object (XYZ type). Now the problem is that fieldSet object that i get inside class extending FieldSetMapper contain empty value for field that are not present in delimited values.
For example:
Suppose that the delimited file format is as follows: |ID|Country|City|Pin|
Suppose i provide following line in file: |1|India|
As you can see the above line does not contain information for City and Pin. Therefore, I expect FieldSet object should contain Null value for these two fiels (City and Pin) instead of empty string. I don't want empty value as Null will help me to know if that field was actually present in file or not.
How can I achieve this ? Do I need to extend DelimitedLineTokenizer which I am using for tokenizing ? Or this is a simple way to do this ?
Any help will be appreciated.
From FieldSetMapper javadoc
To customize the way that FieldSet values are converted to the desired
type for injecting into the prototype there are several choices. You
can inject PropertyEditor instances directly through the customEditors
property, or you can override the createBinder(Object) and
initBinder(DataBinder) methods, or you can provide a custom FieldSet
implementation.
Depending on type of your target bean conversion is done using default Spring convention. If you need other type of logic write your own.

Am trying to compare String with BigDecimal in openjpa using Criteria builder

list.add(build.equal(bondRoot.get(Bond_.writingCompanyCode),dtlRoot.get(ArPstdDtl_.companycd ).as(String.class)));
but am getting the following error:
Caused by: org.apache.openjpa.persistence.ArgumentException: No metadata
was found for type "class java.lang.String". The class is not
enhanced.
Could someone help me out on this ??
Changing type from BigDecimal to String needs conversion, not a cast. Method as cannot convert from type to other - it is purely for typecasting, as documented:
Perform a typecast upon the expression, returning a new expression
object. This method does not cause type conversion: the runtime type
is not changed. Warning: may result in a runtime failure.
Criteria API does not offer conversion from BigDecimal to String. Database vendor specific functions can be used via CriteriaBuilder.function.

Serializing data using IEnumerable<T> with WebGet

possible duplicate:
Cannot serialize parameter of type ‘System.Linq.Enumerable… ’ when using WCF, LINQ, JSON
Hi,
If my method signiature looks like this, it works fine.
[WebGet]
MyClass[] WebMethod()
If the signiature looks like this
[WebGet]
IEnumerable<T> WebMethod()
I get the following error:
Cannot serialize parameter of type 'X.Y.Z.T+<WebMethod>d__2c' (for operation 'WebMethod', contract 'IService') because it is not the exact type 'System.Collections.Generic.IEnumerable`1[X.Y.Z.T]' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute.
I have tried adding.
ServiceKnownType(typeof(IEnumerable))
Same error.
Is this a bug in 2010 beta 2, or is this likely to be correct going forward?
Thanks
The iterator types generated by the C# compiler are not serializable and never will be.
If you read this page, you'll see that it wouldn't make sense to serialize the iterator.
You need to return an array.
EDIT: The simplest way to do that is to move your iterator to a seperate method, and change WebMethod to
[WebGet]
MyClass[] WebMethod() { return OtherMethod().ToArray(); }
I've run into the same issue, and in my case it's simply not possible to change my entire object graph from iterator-based IEnumerable to concrete types. I simply cannot afford the memory to convert over to concrete types like List or Array. Additionally, what about the case where I return an IEnumerable of some object that has an IEnumerable property. It is unacceptable that I have to recurse my entire object graph converting all IEnumerables.
I don't see any good reason why the DataContractSerializer can't iterate any IEnumerable type and render its elements to XML in the same manner as any other collection type, even if the IEnumerable doesn't have a concrete backing type.
This is a bug which should be fixed.