Android. Setting default value for invalid json field - java

I am using retrofit and json converter. I create POJO which contains some fields. One of these fields should be int, but if the server sends something else instead of int, I need to write the default value in this field, for example 2. Now if the server sends something other than int, I get an exception (json malformed exception) in my request, since in the POJO the field is declared as int. Is it possible to write a default value in this field if the server sends something other than int.

in case the server is sending you null you can use Int to store the value, otherwise the parsing won´t be successful; on the other hand if you require a different value other than null as a default you can override your set or get method; set to if is null set the value to zero or get if the value is null return 0.
Hope it helps to debug your problem, if the server is sending you multiple values like strings, booleans, etc. You should discuss which value is the one that you need.

Related

Ignore Field in json in assert operation

I'm trying to write a MUNIT test case for mule service and want to ignore a field having timestamp. Currently, i'm using below code to perform the compare.
org.skyscreamer.jsonassert.JSONAssert.assertEquals(getResource('json/item-locations.json').asString(), payload, false);
I want to ignore field "creationDateTime" which is available in my json message.
Just delete the field from the message before comparing it. If you want to make sure it's actually there, then replace it with a constant value, but only if it exists.

Best practices regarding empty fields and nulls in postgresql

I'm writing a simple webapp to show my coding skills to potential employers. It connects with an API and receives a JSON file which is then deserialized using Jackson and displayed in a table form in the browser. I want to enable the user to persist the Java object in a Postgres database using Hibernate. I got it to work and it does the job nicely but I want to make it more efficient.
Whenever there is no data in the JSON response to put in the object's field (right now all the possible JSON attributes are present in the Java class/Hibernate entity in the form of String fields) I put an empty String ('') and then, with all fields having something and no null objects, it is stored in the database.
Should I only store what I have and put no empty strings in the DB (using nulls instead) or is what I'm doing now the right way?
Null is an absence of a value. An empty string is a value. But that don't impact much to memory. If you want to display data repeatedly and don't want conversion from null to empty string while retrieval you can go for empty string ''.
But if you want unique constraint for values other than empty string '' then use null.
Sometimes null and empty '' can be used to differentiate either data was known or not. for known but not available data use empty and for unknown data null can be used.
Use NULLwhen there isn't a known value.
Never use the empty string.
For example, if you have a customer which didn't supply his address don't say his address is '', say it is NULL. NULL unambiguously states "no value".
For database columns that must have a value for your web application to work, create the backing table with NOT NULL data constraints on those columns.
In your unit tests, call NULL, ..._address_is_null_ and test for success or failure (depending on if the test should trigger no errors or trigger an exception).
The use of '' in databases as a sentinel, a special value that means something other that '', is discouraged. That's because we won't know what you meant it to mean. Also, there might be more than one special case, and if you use '' first, then it makes restructuring more difficult to add others (unless you fall into the really bad practice of using even more special strings to enumerate other special cases, like "deleted" and so on).

Flink: how to write null values as empty through writeAsCsv in sink

I have my a pojo which I create on runtime and there could be null values in the pojo object. When I try to write the object values in a CSV file with dataset.writeAsCsv, the following exception appears:
org.apache.flink.types.NullFieldException: Field 0 is null, but expected to hold a value.
In this case my integer is null. but same is the case with Date.
Is there any way to write null values as empty back to CSV output file?
Since you can only call the writeAsCsvmethod on Datasets of Tuples, there must be a place in your code where your Dataset<Pojo> is transformed into a Dataset<TupleN>.
Tuples can hold null values, but are not serializable when holding them. (The javadoc more or less warns about this.) If you look at the surrounding lines of your exception you may find that it is thrown at serialization, at least this is what I could reproduce:
org.apache.flink.types.NullFieldException: Field 1 is null, but expected to hold a value.
at org.apache.flink.api.java.typeutils.runtime.TupleSerializer.serialize
So I guess you will have to determine what a null value means in the domain of your POJO and replace it accordingly before you write your CSV file. A solution could be to transform your values to Strings and replace null with "". However, depending on the meaning of the value other substitutions could be more appropriate.

Sending optional parameters as empty in a web-service requestbody which do not have values

I am trying to consume some rest webservices with json request and
response format. I am sending all the values in the request even the
optional parameters which do not have values.(The default value for
all the optional parameter I set as empty string "" to avoid null).
One of my colleague pointed out to check empty parameters for optional
values and remove them from the request if no value exist.
I know I would be stupid to ask this very basic question but I would like to know the best practice:
Is it good to send empty parameters or we need to check for empty
parameters and send only the one which has value.
Generally the best practise is to follow the API.
But if removing optional parameters which do not have values does not affect the target consumers of the API, then you can go for it.
You can check the answer to following post, to get some good reasons to keep the null fields.
Is it Worth To exclude null Fields.

How to add primitive property to existing java datastore entity to avoid null pointer exception

I have existing entities of a type in my gae datastore to which i want to add a new primitive property of primitive type "long":
#Persistent private long bests = 0;
When I do this, when i try to load existing entities which obviously don't have this property set yet, i get:
java.lang.NullPointerException:
Datastore entity with kind Player
and key Player("patrick") has a
null property named bests. This
property is mapped to
model.Player.bests,
which cannot accept null values.
What can I do to avoid this problem? Like a way to default to zero when field is not existent? I want to avoid using class Long, and stick with using primitive long.
You could use a Long temporarily, update all of your entities to have a value of zero, and then change the field back to a long. Alternatively, read in all of your data to some file, delete all of your entities, and write them back with the new long field (careful of ownership links being broken).
java type of Long accepts nulls, long doesn't. Existing data won't have that so are null.

Categories

Resources