Convert a map to a JSON stringt - codenameone

I will like to convert a map to a valid JSONOBject or json string. I tried Result.fromContent(map).toString() which seems to work only if map is a valid Hashtable ( no null values). Is there another way?
This is the NPE I get
java.lang.NullPointerException
at com.codename1.processing.PrettyPrinter.toString(PrettyPrinter.java:178)
at com.codename1.processing.PrettyPrinter.print(PrettyPrinter.java:69)
at com.codename1.processing.PrettyPrinter.print(PrettyPrinter.java:60)
at com.codename1.processing.MapContent.toString(HashtableContent.java:113)
at com.codename1.processing.Result.toString(Result.java:314)
at userclasses.StateMachine.onEditorSave(StateMachine.java:284)
at generated.StateMachineBase.processCommand(StateMachineBase.java:257)
at com.codename1.ui.util.UIBuilder.processCommandImpl(UIBuilder.java:1634)
at com.codename1.ui.util.UIBuilder.access$100(UIBuilder.java:85)
at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2757)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:345)
at com.codename1.ui.Form.actionCommandImplNoRecurseComponent(Form.java:1337)
at com.codename1.ui.Button.fireActionEvent(Button.java:389)
at com.codename1.ui.Button.released(Button.java:424)
at com.codename1.ui.Button.pointerReleased(Button.java:512)
at com.codename1.ui.Form.pointerReleased(Form.java:2353)
at com.codename1.ui.Form.pointerReleased(Form.java:2286)
at com.codename1.ui.Component.pointerReleased(Component.java:2459)
at com.codename1.ui.Display.handleEvent(Display.java:1892)
at com.codename1.ui.Display.edtLoopImpl(Display.java:994)
at com.codename1.ui.Display.mainEDTLoop(Display.java:925)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

The code:
Result.fromContent(map).toString()
Works for Hashtable/HashMap semantics where null values aren't present so they are ignored. If you just want the null values to not appear you can just convert the Map to a HashMap which won't include null values.
If you are getting an exception I suggest posting the stack trace as part of the question and we might be able to fix that.

Related

percent encode out of range exception

I have a problem with percent.encode() in package:convert/convert.dart package.
I have an API that is used by the Arabs and can contain Arabic characters. One of the Arabic characters is "خ" and if I want to convert it with this method percent.encode('خ'.codeUnits). The code unit number is 1582 which represents 0x62e in hexadecimal. In this case, I will get an exception because it's out of range of the bytes that this library can convert. and I have this exception Unhandled Exception: FormatException: Invalid byte 0x62. Can you please help me with my problem? are there any alternatives I can use?
I have found a solution, I've used Uri.encodeQueryComponent(data). It did the trick.
[Update 1]
There is an alternative way
percent.encode(utf8.encode('خ'))

Parameterize() must be of the type array, string given in Laravel 5.2

i am using laravel 5.2 and trying to update records using whereIn('id',[1,2]) but when i try to pass a json value [1,2] to it , i returns
parameterize() must be of the type array, string given. I am mentioning my code below.
$load_id=json_encode($request->chk_load,JSON_NUMERIC_CHECK); // it returns [1,2]
Load::whereIn('id',$load_id)->update(array('status'=>3));
What should i do to fix this error. ?
seems json_encode($request->chk_load,JSON_NUMERIC_CHECK);
returns json string and not an array..
can you elaboarate $request->chk_load is what kind of data it is?

Simpleframework serialize elements name without conversion

I have to create XML from Java objects and I use the Simple framework.
My problem is I need to send some names in camel case:
#Element
String ChannelData
and the xml element produced is:
<channel-data>
Which is rejected by the receiver, it needs to be
<channelData>
I cannot find a way to configure this, I tried adding the name explicitly:
#Elenemt(name="channelData")
but without success.
Ok I solved it.
I actuallt initialized the Persister with the wrong format:
Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH);
Instead of:
Format format = new Format(0, null, new CamelCaseStyle(), Verbosity.HIGH);

Error with ibatis sql iterate

I'm using ibatis + DWR , but when i pass a map to ibatis i will get an error as below:
Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or
property was not a Collection, Array or Iterator.
here is my sql:
<update id="updateDarenFlagByUserTagIDs" parameterClass="java.util.Map">
update system_usertag
set isdaren = 1
where uid = #uid#
<isNotEmpty prepend=" AND " property="utidlist">
and utid in
<iterate open="(" close=")" conjunction="," property="utidlist">
#utidlist[]#
</iterate>
</isNotEmpty>
</update>
and here in the DWR part, i passed a map as below:
{'uid':uid, 'utidlist':utidlist}
Any ideas on this error?
I have answered the exact same question in the following post https://stackoverflow.com/questions/18997883/malformed-url-exception-in-ibatis/19025819#19025819 do make reference to it. The solution to your problem is very simple, ensure that the argument nested in your iterate tag <iterate></iterate> which in your case is "utidlist" is indeed a list/collection and nothing but a list. The cause of this error is that your iterable property "utidlist" isn't a list. If you can get it to be a list and not a flat value you'll just be fine.
Just in case you can still get it to work you may also want to paste a full stack of your logs so that you can see what is going wrong.

How to retrieve field name in case of unmarshalling error in CXF?

Precondition: service based on CXF receives request/response with data, which violates XSD restriction.
Actual behavior:
In this case CXF returns fault with message like:
cvc-maxLength-valid: Value 'string_length_violated_value' with length = '28' is not facet-valid with respect to maxLength '13' for type 'XSDStringTypeWithLengthRestriction'
Goal:
return fault to consumer with name of field which contains invalid data. F.e. something like this:
Response from provider contains invalid data. Value 'string_length_violated_value' of field 'field_name' is not facet-valid with respect to maxLength '13'.
I'm wondering if it is possible and if so, then how to determine (where to retrieve from) this field name?
I'm not sure if this will completely work, but you can give it a try:
You can create a JAXB ValidationEventHandler and register that on your endpoint.
The ValidationEvent that it gets has the basic string (that you see above) and other information. I would put a breakpoint in there and dig into the event to see if ANY additional and useful information is available.

Resources