How to declare entity property as array in JDL-Studio? - java

Example:
entity record {
id Long,
description String,
url String,
file Byte[]
}
I'm asking how I can declare the file property without an error.
Currently I get:
MismatchedTokenException: Expecting --> '}' <-- but found --> '[' <--

Related

Convert string to json object giving error

I have a string which needs to be converted to JSONObject, I added the dependency, but I'm getting error, which I'm not able to figure out. I have the following dependency:
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
String s ="{name=Alex, sex=male}";
JSONObject obj = new JSONObject(s);
System.out.println(obj.get("name"));
I'm getting an exception:
org.json.JSONException: Expected a ':' after a key at line 5
The JSON you've provided is not valid because separator colon : should be used a separator between a Key and a Value (not an equals sign).
A quote from the standard The JavaScript Object Notation (JSON) Data Interchange Format:
4. Objects
An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string. A single colon comes after each name, separating the name
from the value. A single comma separates a value from a following
name. The names within an object SHOULD be unique.
You can preprocess you JSON before parsing it using String.replace()
String s ="{name=Alex, sex=male}";
JSONObject obj = new JSONObject(s.replace('=', ':'));
System.out.println(obj.get("name"));
Output:
Alex
Also, note that Org.json (as well as some other libraries like Gson) would take care of fixing missing double quotes ". But it would not be the case with libraries like Jackson.
The string you're assigning to the variable s is not valid JSON. Property names and properties should be separated by : instead of =, and double quotes should be used around strings and property names.
So the string in your example should be like this (with \ being used to escape the quote characters within the string quotes):
String s = "{\"name\":\"Alex\",\"sex\":\"male\"}";
You should use : instead of =
String s = """{"name":"Alex","sex":"male"}"""; (Since Java 13 preview feature)

Why Backslash character not removed even after using replace method

I have made a rest request which is returning me a Set in JSON format which is "[\"TestBack\"]".
If I directly parse it in Apex using
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody(),Set<String>.class);
then I get following error
FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
but if I explicitly remove double quote sign by using
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody().substringAfter('"').substringBeforeLast('"'),Set<String>.class);
I got following error
FATAL_ERROR System.JSONException: Unexpected character ('\' (code 92)): expected a valid value
and if I try to use replaceAll method
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody().substringAfter('"').substringBeforeLast('"').replaceAll('\\',''),Set<String>.class);
it shows following error
FATAL_ERROR System.StringException: Invalid regex: Unexpected internal error near index 1
Is there any way to get parse back into Set?

Read not correct JSON

I have JSON as string
"{nameBitsCount=131}"
I need, using Jackson: 1) Parse this JSON correctly. 2) Put result into Map<String, Long>
But I Getting exception.
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: (String)"{nameBitsCount=131}";
PS: I think I need to change style of JSON to this
"{\"nameBitsCount\":\"131\"}";
The JSON you send isn't valid
{nameBitsCount=131}
need to convert format :
{"nameBitsCount":131}
add {"} around the Key and change {=} to {:} the JSON format valid for Json Object :
{
"Key" : "Value"
// "VALUE" if use for String and char put {"} around the value VALUE ex: 0.0 , 1 , -50 , FALSE
}
Look this website :
JSON Website
JSON Syntax

messageSource doesn't replace all arguments in template

I have code:
messageSource.getMessage("some.key",new Object[]{30,31},Constants.LOCALE)
and key inside property file:
some.key=Csv header length ({0}) doesn't correspond the mapping file size {1} .
but result is strange:
Csv header length (30) doesn't correspond the mapping file size {1} .
Fisrt variable was successfully replaced but second - not.
Why does second argument was not resolved?
The problem is because you have a single quote in the message that you have not escaped.
See https://www.mscharhag.com/java/resource-bundle-single-quote-escaping for an example of your problem.

How do I get the value of a key if it contains a space in Rest Assured / Serenity?

I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:
{"Entry ID" : "654123"}
I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
This produces the error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
# line 1, column 33.
Entry ID
^
1 error
I have tried wrapping the "Entry ID" term in different ways to no avail:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
Is it possible to get the value of a key that contains a space in Rest Assured?
You just need to escape the key with single quotes:
then().body("'Entry ID'", not(isEmptyOrNullString()))
Here's an example (tested in version 3.0.6):
// Given
String json = "{\"Entry ID\" : \"654123\"}";
// When
JsonPath jsonPath = JsonPath.from(json);
// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));

Categories