java.util.Date unmarshalling form grails json request - json

I'm facing following error while trying unmarshal java.util.Date from JSON in grails controller.
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '2011-10-07 10:24:40' with class 'java.lang.String' to class 'java.util.Date'**
Also, I've tried the following method but still no luck, actually I've doubt wether I've implemented following in right way or not because when i put println statements in following method:
public CustomDateBinder(List formats)
Nothing prints on console.
Grails Date unmarshalling

According to the description of your error message you are trying to convert a String to Date, if you want to do it manually you can use the following method in your Controller (since Grails 2)
def val = params.date('myDate', 'dd-MM-yyyy') //Obviously you need to change the format
Check the following post for more info: http://mrhaki.blogspot.com/2012/01/grails-goodness-date-request-parameter.html

Related

Json convert TimeSpan Error .Net Core 2.2

i work with .net core 2.2, i tried to deserialize datas from WebApi but TimeSpan Type is not converted.
My InnerException is :{"Could not cast or convert from System.String to System.TimeSpan."}
My ErrorMessage is :{"
Error converting value "PT20H20M" to type 'System.TimeSpan'. Path 'value[0].StartTime', line 1, position 179."}
the problem is on a member of my class:
public TimeSpan StartTime { get; set; }
Can someone get the same error or can someone give me some ideas to how manage the problem.
Thanks !
Well, I guess the error is that the default converter does not know the format that you are trying to send in. You have to use a format that it understands or use a custom converter or use a string and convert it yourself afterwards.
If you run TimeSpan.Parse("PT20H20M") you will get an error that says it's not a valid format.
It seems that this call can get you a TimeSpan:
System.Xml.XmlConvert.ToTimeSpan("P2DT01H")
You can use a custom converter as described here or here (First one is for .net core 3).

C# - Validate a json against a Schema. Where the error occurs in the json?

in c# I validate a json with a json schema, using the library Newtonsoft.Json.
JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject Json= JObject.Parse(json);
bool valid = Json.IsValid(schema);
It works fine, but the problem is that I don't understand where the errors occur in a simple way. My 'json' is only in one Line and I have a lot of property so it isn't immediate to find the position of the proprierty where the error occurs.
For example, I got the error:
'Invalid type. Expected String but got Null. Line 1, position 2221'
Is there a way to find the name of the property of my json where the error occurs using the library Newtonsoft.Json?
Thanks a lot

Scala, Json & Spring Boot Rest

I am building a Rest API using spring boot and Scala based on the below suggestion. I am successfully able to call the methods in Scala but the case class is not working the way it is designed.
When I try to create a Json using net.liftweb.json, I am getting additional string as "$outer":{}. I don't want this additional string in my output.
Note that the spring boot is expecting class instead of object. I suspect this could be an issue. Can you help me on this.
My case class looks like this:
case class BasicSrch(size: String, query: Match)
Erroneous output
{"$outer":{},"size":"10","query":{"$outer":{},"match":{"$outer":{},"_all":{"$outer":{},"query":"SNOW","operator":"and"}}}}
Expected Output
{"size":"10","query":{"match":{"_all":{"query":"VALLE","operator":"and"}}}}
If you are writing UTF-8 encoded JSON to the byte array or OutputStream then try jsoniter-scala: https://github.com/plokhotnyuk/jsoniter-scala
It will not serialize any other fields except those which are defined in a primary constructor of the case class.

JSON.registerObjectMarshaller doesn't work in bootstrap

When I want to test a customized "render as JSON " method, I used the following code
class BootStrap{
def init = { servletContext ->
....
println "change the json before format"
JSON.registerObjectMarshaller(Date) {
println "JSON DATE MARSHAL"
return it.format("yyyy-MM-dd HH:mm:ss")
}
println "change the json after format"
}
}
but what printed in the console is like this:
| Compiling 1 source files
| Running Grails application
Active MQ start. ConsumerURL is failover:ssl://xxx.
change the json before format
change the json after format
|Server running. Browse to http://xxx
the strange thing is that the "JSON DATE MARSHAL"didn't be printed.
But it worked when I put the code in a controller.
I don't know what happened.
Any suggestion will be appretiated.
Update:
Like railsdog said, it seems the closure in the init didn't work.
What I supposed the date format in JSON is like this:2016-12-15 16:44:21
But what I get is 2016-12-15T08:44:21Z.
When I put the marshaller in controller, it worked, and the date format in JSON is as expected:
2016-12-15 16:44:21.
I also get the console output:"JSON DATE MARSHAL".
I would assume that registering the object marshaller doesn't actually execute/invoke the closure, hence no debug is emitted.
Instead of registering marshaller check out property grails.databinding.dateformats in your grails application config. You can add there more different date formats which should be handled by your app.

Yii2 REST DB Geometry field serialization

I have a table in mySQL with fields of GEOMETRY and POLYGON types. And the Yii2 RESTful app. Here is a standart ActiveController:
<?php
namespace backend\modules\v1\controllers;
use common\models\Geo;
class GeoController extends ActiveController
{
public $modelClass = 'common\models\Geo';
}
When I have any records in my table with spatial data JSON response breaks with message:
code: 5
file: "/vendor/yiisoft/yii2/helpers/BaseJson.php"
line: 120
message: "Malformed UTF-8 characters, possibly incorrectly encoded."
name: "Exception"
Should I write custom json serializer or somehow override ActiveProvider select with something like "AsText(geometry_field_name)"?
I think you should change field types supported by JSON (http://www.tutorialspoint.com/json/json_data_types.htm).
For example, if you use MySQL field with type CHAR, you will get the same error
Basically the issue was solved by extending ActiveQuery class with adding additional column using SQL command AsText(geomery_field_name) as geometry_text.