JSONObject how to store null value - json

I am using com.liferay.portal.kernel.json.JSONObject in liferay 6.2 and I want to store null value in an object of this type as mentioned below
`JSONObject jsonObject = JSONFactoryUtil.createJSONObject();`
`jsonObject.put("name","varun")`
`jsonObject.put("address",null)`
As in org.json.JSONObject we can set null by JSONObject.NULL
Is there any way to set null in liferay JSONObject?

The Liferay object wraps only the com.liferay.portal.json.JSONObjectImpl
As Tobias said you can use the JSONObject.NULL
Another thing is, do you really need to set this? Usually, parsers just null the fields if they don't find them.

Related

Null check operator used on a nullsis value flutter

I have problem with null check operator and i using firebase firestore or json model
And i using the method to get data user from model
But while i return he give me null check operator help!

Spring Boot JSON Serialization

I'm using Spring Boot 1.3.3 and created a REST controller to add a JSON object into Mongo DB collections.
The data to be added from the JSON object will be a subset of information received from the request. So i have created a JSON request object ( DTO ) and an entity object ( model ) to be stored in Mongo collection.
I'm facing an issue now as the JSON request object is populated with default values for integer ( 0 ) and boolean data types ( false ) even if these fields are not populated as part of the request message. I don't want to store these values in the database.
I have added " spring.jackson.serialization-inclusion=non-null " and " spring.jackson.serialization-inclusion=non-default " properties in my application.properties file but still the fields are populated with default values.
Could anyone please help me out in resolving this issue and bypass the default values. NOTE: It works fine for String data type as they would be NULL values by default if not created.
Thanks in advance
String Attributes accept the null value while the primitive attributes have a default value for example 0 is default value for the int attributes.. to avoid having this values Use Integer instead.
Please use this annotation above your fields in bean class with which you facing the problem and tell me your problem is solved.
'#JsonInclude(Include.NON_NULL)'
Thanks

Update empty string to NULL in a html form

I'm building a site in Laravel.
I have foreign key constraints set up among InnoDB tables.
My problem is that if i don't select a value in a, say, select box, the framework tries to insert or update a record in a table with '' (empty string). Which causes a MySQL error as it cannot find the equivalent foreign key value in the subtables.
Is there some elegant way to force the insertion of NULL in the foreign key fields other than checking out every single field? Or to force MySQL to accept '' as a "null" foreign key reference?
In other words: I have a, say, SELECT field with first OPTION blank. I leave the blank OPTION chosen. When I submit, an empty string '' is passed. In MySQL apparently I can do UPDATE table SET foreignKey=NULL but not UPDATE table SET foreignKey=''. It does not "convert" to NULL. I could check the fields one by one but and convert '' to NULL for every foreign key, maybe specifying all of them in an array, I was wondering if there's a more streamlined way to do this.
Maybe have to change my ON UPDATE action (which is not set) in my DB schema?
Edit: the columns DO accept the NULL value, the problem is in how the framework or MySQL handle the "empty value" coming from the HTML. I'm not suggesting MySQL "does it wrong", it is also logical, the problem is that you can't set a "NULL" value in HTML, and I would like to know if there's an elegant way to manage this problem in MySQL or Laravel.
In other words, do I have to specify manually the foreign keys and construct my query accordingly or is there another robust and elegant way?
My code so far for the model MyModel:
$obj = new MyModel;
$obj->fill(Input::all())); // can be all() or a subset of the request fields
$obj->save();
At least since v4 of Laravel (and Eloquent models), you can use mutators (aka setters) to check if a value is empty and transform it to null, and that logic is nicely put in the model :
class Anything extends \Eloquent {
// ...
public function setFooBarAttribute($value) {
$this->attributes['foo_bar'] = empty($value)?null:$value;
}
}
You can check out the doc on mutators.
I've been oriented by this github issue (not exactly related but still).
Instead of using
$obj = new MyModel;
$obj->fill(Input::all())); // can be all() or a subset of the request fields
$obj->save();
Use
$obj = new MyModel;
$obj->fieldName1 = Input::get('formField1');
$obj->fieldName2 = Input::has('formField2') && Input::get('formField2') == 'someValue' ? Input::get('formField2') : null;
// ...
$obj->save();
And make sure your database field accepts null values. Also, you can set a default value as null from the database/phpmyadmin.
You must remove the "not null" attribute from the field that maps your foreign key.
In the model add below function.
public function setFooBarAttribute($value)
{
$this->attributes['foo_bar'] = $value?:null;
}

How to pass empty or null date in JSON for WCF REST Web service

We have JSON object as follows.
{
"AdvanceNotificationDays":null,
"Egos":[],
"Interests":[],
"Name":"Birthday gift for Ruchir",
"Occasion":null,
"OccasionDate":"\/Date(null)\/",
"ProfileId":null,
"UserId":4350,
"WishlistId":0,
"isShared":false
}
We try to pass this JSON object to the server and everything gets parsed except the Date. For date, the JSON parser throws the exception that says "Cannot parse null to Int64". We just want to pass empty date or null date to the server. Can anybody please help? We googled for solution to this issue, did not find an answer so far.
Why don't you just set OccasionDate to null?
try making OccasionDate of type DateTime? so the date can be null.

Linq to SQL null values in GridView

Normally if I'm linking an ObjectDataSource to a GridView and I have a TemplateColumn that has an Eval in it and it's Null, I can just put a ".ToString()" it works fine. For some reason, this doesn't work the same when you're using Linq to SQL.
I originally was using XSD files for my DAL with a custom BLL. I tied it to the GridView with an ObjectDataSource. I'm in the middle of swapping out the XSD files with Linq to SQL and everything is working just like the old way except for the columns that can have Null values.
Has anyone run into this before and if so, how do I work around this problem?
Most everything that LINQ returns is of Nullable types. So in your binding expressions you need to use GetValueOrDefault().ToString() or the new "??" null coalescing operator rather than just plain old ToString(). I hope this helps. Check this link out to.
Example:
// this will output the int if not null otherwise an empty string.
<%# (int?)Eval("MyIntegerField") ?? "" %>