How to set a default value for empty fields in Moshi - moshi

I have a model written in Kotlin, where all the fields are non-nullable. how do i set a default value for null JSON fields.

Moshi will throw a JsonDataException if a Non-null field in the class is null when parsing the JSON.
You could either change your class to set fields to defaults after the JSON has been parsed as detailed here:
https://github.com/square/moshi/issues/762#issuecomment-471422238
Alternatively you can create your own custom adapter which handles null values. The example below checks whether a JSON value is null and then sets it to an empty string.
Moshi/Kotlin - How to serialize NULL JSON strings into empty strings instead?

Related

NiFi ifelse invalid because returning a string instead of a boolean

I am working on a NiFi workflow. JSON is coming into the workflow and I am using EvaluateJsonPath and RouteOnAttribute processes. In the EvaluateJsonPath, I have the Null Value Representation set to empty string.
[
{
'a':null,
'b':null
},
{
'a':'1',
'b':'2'
}
]
I use a JsonSplitter to split the array so each element in the JSON array is handled separately.
In the first EvaluateJsonPath I do the following.
a=$.a
b=$.b
In the RouteToAttribute (I am using the same property, if I use a different or new property, I get the same issue)
a=${a:isEmpty():ifElse('NOT PROTECTED', 'PROTECTED')}
b=${b:isEmpty():ifElse('', 'ACTIVE')}
The RouteToAttribute processor is providing the following error for both properties:
'a' validated against '${a:isEmpty():ifElse('NOT PROTECTED', 'PROTECTED')}' is invalid because Expected Attribute Query to return type BOOLEAN but query returns type STRING
'b' validated against '${b:isEmpty():ifElse('', 'ACTIVE')}' is invalid because Expected Attribute Query to return type BOOLEAN but query returns type STRING
It's really not clear what you are trying to do.
You are using RouteOnAttribute, but it looks like you are trying to replace attribute values. This is not what RouteOnAttribute does.
RouteOnAttribute is designed to evaluate a query, and then route matching FlowFiles to a specified relationship.
With RouteOnAttribute you have 3 options for the Routing Strategy which are
Route to Property name
Route to 'matched' if all match
Route to 'matched' if any matches
When using option 1, that is, Route to Property name, the NAME of the property is the RELATIONSHIP that the FlowFile will be routed to. The VALUE of the property is a QUERY that must return a BOOLEAN (true or false). If the result of the query is True, the FlowFile is routed to this relationship. If you have multiple properties configured, it will evaluate them in order.
When using option 2 or 3, you do not specify the name of the relationship, it is always called matched. You can specify multiple properties, and the NAME of the property is irrelevent. The VALUE of the property is a QUERY that must return a BOOLEAN. With option 2, if ALL of the queries return True, the FlowFile is routed to matched. With option 3, if ANY of the queries return True, the FlowFile is routed to matched.
Read the documentation.
Your error is caused because ${a:isEmpty():ifElse('NOT PROTECTED', 'PROTECTED')} does not return a BOOLEAN. It returns a STRING. There is nothing RouteOnAttribute can do with this result.
Either way, you would be better off looking at Records.
Specifically, if you want to update the value of fields inside the data, look at UpdateRecord.
If you want to do routing logic on fields inside the data, look at QueryRecord and PartitionRecord.
Splitting each JSON element out using SplitJson is inefficient and avoidable when using Records.

How to check a field value is must be a string when it filled out in laravel 5.4 validator

I have a field called middle name which is an optional field. I want to check this field if it has some value otherwise I dont want to check this field value.
How can I do this in laravel 5.4 validator.
String is an object type its not primitive type like integer or char. I didn't check how laravel handles this validation but based on documentation you have to put nullable. Probably because string returns an object with empty attribute. so #Gaurav Gupta's answer not going to work.
The field under validation must be a string. If you would like to
allow the field to also be null, you should assign the nullable rule
to the field.
'middle_name' => 'nullable|string'
you can just simply write
'middle_name' => 'nullable|string',
in your validation array it check only when some value exits .Means it not required value

overload get to use different routes

I have a single ApiController: recordsController and I have the basic 4 CRUD methods in it, I need to somehow have the ability for the user to do something like:
records/users - this is achieved by the default Get method
records/users/Male
records/users/White
I am brand new to webapi
This is the error I am getting trying to do:
http://localhost/records/Male
The request is invalid.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'RESTServices.Controllers.recordsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Coredata NSFetchRequest DictionaryResultType null properties Swift

Hello guys i am using this code to fetch the Result from Coredata
func getRequest(entiryDesc:NSEntityDescription) -> NSFetchRequest{
var request:NSFetchRequest = NSFetchRequest()
request.entity = entiryDesc
request.resultType = NSFetchRequestResultType.DictionaryResultType
return request
}
Now the problem is i need All the attributes which contains the Nil value too but the excutefetchrequest returns only those properties which have values , is there any work around for this to return Null attributes with String like "" every time i fetch ? Thanks Advanced
Of course, you can just dispense with the .DictionaryResultType and fetch normal managed objects. There are very few cases where the dictionary result type makes sense.
If you want to construct a dictionary with all attributes filled out (for whatever opaque reason), remember two things:
Make sure to insert the null values as objects NSNull()
You can use the NSEntityDescription API to generate all the attribute keys. Use entityDescription.propertiesByName.allKeys to generate a list of all attribute names of your entity.

Loading null values with CsvDataFileLoader in dbunit

We are using the CsvDataFileLoader to load in our reference data like so:
new InsertIdentityOperation(DatabaseOperation.CLEAN_INSERT)
.execute(connection,
new CsvDataFileLoader().load("/sql/ReferenceData/"));
Is there anyway to put null values into a csv that is loaded into our db.
I don't think there is, I would imagine that , null and NULL would all get interpreted as their string values.
Has anyone managed to do this or know of a work around for this problem?
CsvDataFileLoader uses CsvURLProducer to load and parse the data.
In that class on Line 145 for dbunit 2.4.8 you see the following:
if (CsvDataSetWriter.NULL.equals(row[col])) {
row[col] = null;
}
CsvDataSetWriter.NULL contains the string "null" therefore your assumption that null would get interpreted as a string value appears to be incorrect and you should use this in your CSV.
Of course this means that you can't have the string "null" in your fields but I'm sure this isn't often required.

Resources