I am having a problem for which I cannot find a solution anywhere in the jBehave documentation. I have a story like this:
Given We have a JSON {boo: <boo>, foo: <foo>}.
When We get this.
Then We shall assert.
Examples:
|boo|foo|
|3|4|
And a step like this:
#Given("We have a JSON {$obj}.")
public void given(#Named("obj") final OwrObj obj) {
// Some code...
}
I also have a custom parameter converter which converts to OwrObj objects. My problem is that the converter gets a String input:
"{boo: <boo>, foo: <foo>}"
instead of:
"{boo: 3, foo: 4}".
In short, what is inside the curly brackets is seen as a parameter value.
Does anyone have any ideas on how to accomplish what I am trying to do. That is, get a custom object from a parametarised JSON structure in the story?
Related
Actually, the main problem is still that there are no reified typeargs for classes in Kotlin. But here is why this bothers me in this specific case:
Suppose you have a wrapper class Wrapper that takes in a string content and a class* type and can output an object of class type retrieved by parsing content as JSON by demand by calling the function getObj():
class Wrapper<T>(private val content: String, /*private val type: KClass<*>*/) {
fun getObj(): T {
// ?
}
}
And I want to use kotlinx.serialization. Now, you might have noticed how I put an asterisk after "class" before. Here's the reason: Yes, Wrapper has to take the target class in some way, but how? Should it be just the typearg (won't work because type erausre) or a KClass reference (won't work because I need a reified typearg)?
The thing is that as far as I know, the only way to decode a generic JSON to a serializable target class is to use Json.decodeFromString<T>(content), where T is the target type and content is the JSON string. Now, T is defined to be reified (so that the type can be processed at runtime) and can only be filled with another reified typearg or an actual class reference. I can't use another reified typearg because I am in the context of a class and a class cannot have reified typeargs. I can also not use an actual class reference because the user of the class should be able to construct it with different targets, e.g. they decide what the target is, not me.
So, how do I do this with kotlinx.serialization? Is it even possible?
Ok so no one answered the question yet, but I also posted this question in the r/Kotlin subreddit. Here it is.
I actually got an answer there (credits to u/JakeWharton), and since you might get across this StackOverflow question because you googled the same question, you might be happy to find an answer here. So here's my try to paraphrase the answer:
So, basically, kotlinx-serialization does indeed not work with KClasses. But when you think about it, you only need the KClass to determine how to serialize it. And since that is determined at compile-time when you work with KXS, you actually just need to pass the serializer (the actual strategy defining how to serialize / deserialize your class). You can obtain a serializer for every class annotated with #Serializable by invoking .serializer() on it; the result will be of the type KSerializer<T>. So, instead of having
class Wrapper<T>(private val content: String, private val type: KClass<T>)
and constructing it via
val wrapper = Wrapper("{}", Foo::class)
You can do it like this:
class Wrapper<T>(private val content: String, private val serializer: KSerializer<T>)
and then construct it like this:
val wrapper = Wrapper("{}", Foo.serializer())
(supposing Foo is annotated with #Serializable)
you can then serialize and deserialize by using the KSerializer instead of a typearg, like this:
val obj: T = Json.decodeFromString(serializer, "[Your JSON String]")
val str: String = Json.encodeToString(serializer, obj)
And that's it! Just swap out your regular (K)Class approach by KSerializer and it'll work with KXS.
Say we have:
class MyClass {
myProperty: string
}
Is there any built in function or easy way to get JSON like this?:
{
"myProperty": "string"
}
EDIT: My end goal is I want to dynamically print typed class definitions to a web view, in some kind of structured object syntax like JSON. I'm trying to make a server API that will return the schema for various custom classes - for example http://myserver.com/MyClass should return MyClass's properties and their types as a JSON string or other structured representation.
Evert is correct, however a workaround can look like this
class MyClass {
myProperty: string = 'string'
}
JSON.stringify(new MyClass) // shows what you want
In other words, setting a default property value lets TS compile properties to JS
If the above solution is not acceptable, then I would suggest you parsing TS files with your classes with https://dsherret.github.io/ts-simple-ast/.
Typescript class properties exist at build-time only. They are removed from your source after compiling to .js. As such, there is no run-time way to get to the class properties.
Your code snippet compiles to:
var MyClass = /** #class */ (function () {
function MyClass() {
}
return MyClass;
}());
As you can see, the property disappeared.
Based on your update, I had this exact problem. This is how I solved it.
My JSON-based API uses json-schema across the board for type validation, and also exposes these schemas for clients to re-use.
I used an npm package to automatically convert json-schema to Typescript.
This works brilliantly.
I have been working on an RCP Client to handle weather data.
What i do is 2 things, first i scraped the JSON i will be using and put it into a dart file. See: https://dartpad.dartlang.org/a9c1fe8ce34c608eaa28
My server.dart page, will import the weather data, and then carry out the following:
import "dart:io";
import "weather_data.dart";
import "dart:convert";
import "package:rpc/rpc.dart";
find ApiServer _apiServer = new ApiServer(prettyPrint:true);
main() async {
Weather w = new Weather(WeatherJson);
TestServer ts = new TestServer(w);
_apiServer.addApi(ts);
HttpServer server = await HttperServer.bind(InternetAddress.ANY_IP_V4, 12345);
server.listen(_apiServer.httpRequestHandler);
}
class Weather{
Map weather;
Weather(this.weather){
Map get daily => weather["daily"];
}
}
#ApiClass(name:"test_server", version: 'v1', description: 'This is a test server api to ping for some quick sample data.')
class TestServer {
Weather myWeather;
TestServer(this.myWeather){
}
#ApiMethod(method:'GET', path: 'daily')
Map<String, Object> getDaily(){
return myWeather.daily;
}
}
So, the server starts correctly, and i will go to localhost:12345/test_server/v1/daily and it will return this:
{
"summary": {},
"icon": {},
"data": {}
}
which is not correct. If you look up the JSON data, summary and icon are both strings and data is an array. They are also empty, and should contain the data i wanted to return.
Why does this occur? Is it because i am returning a Map<String, Object>? I was trying to set it up to be: Map<String, dynamic> but the dart compiler didnt like it.
How do i get this data to return the correct dataset?
The Dart website for RPC is located at: https://github.com/dart-lang/rpc
and you can see that under methods, the return value of a method can be either an instance of a class or a future. That makes sense as per usual, so I set it to be a Map<String,Object> though trying to be vague about it by saying: Map was not sufficient.
Edit:
When doing this mostly in dart pad without RPC, it seems to work correctly, by a sample of: https://dartpad.dartlang.org/3f6dc5779617ed427b75
This leads me to believe something is wrong with the Parsing tool as it seems the return type in dartpad allows to return Map, Map<String, Object>, and Map<String, dynamic>.
Having had a quick look at the RPC package README here https://pub.dartlang.org/packages/rpc, it seems that methods marked as Api methods (with #ApiMethod) should return an instance of a class with simple fields such as:
class ResourceMessage {
int id;
String name;
int capacity;
}
The RPC package will take that instance and serialize it into JSON based on the field names.
From the README:
The MyResponse class must be a non-abstract class with an unnamed
constructor taking no required parameters. The RPC backend will
automatically serialize all public fields of the the MyResponse
instance into JSON ...
You are returning a nested Map representation of the JSON you want the RPC operation to emit and would guess that the RPC package does not handle it as you are expecting it to.
Re: this from your question:
This leads me to believe something is wrong with the Parsing tool as
it seems the return type in dartpad allows to return Map, Map, and Map.
There is no 'parsing' on JSON going on on your example. The data you have is a set of nested literal Dart Maps, Lists and Strings with the same structure as the JSON it was derived from. It just happens to look like JSON.
In your example you are just selecting and printing a sub-map of your data map (data['daily']), which prints out the String that results from calling toString() - which is recursive so you get the contents of all the nested maps and lists within it.
So it's not a 'deep copy' issue, but a difference in how toString() and the RPC code processes a set of nested maps.
BTW: the return type of your getDaily() method is immaterial. What is returned is just a Map whatever the declared return type of the method is. Remember types in Dart are optional and there for editors and compilers to spot potentially incorrect code. See https://www.dartlang.org/docs/dart-up-and-running/ch02.html#variables.
I am going to piggyback off of #Argenti Apparatus here as there was a lot of information gained from him.
Long story short, the required return type of the method:
#ApiMethod(method:'GET', path: 'daily')
Map<String,Object> getDaily(){ // <-- Map<String,Object>
return myWeather.daily;
}
is the error.
I went through and updated the method signature to be Map<String,String> and it parsed it entirely correct. It did not parse the object as a string, but actually parsed it as a full recursed object.
I went through and for the sake of code cleanliness also changed signatures of Weather properties to reflect what they actually were, Map<String,Object> as well.
All in all, When defining it to be an value type of Object, it was returning curly braces, but setting it as a String parsed it correctly.
I ran it through JSLint to confirm it is correct as well.
I gave a +1 to the helper, I had to dig deeper into the code to see WHY it wasnt doing a Map correctly.
This also I feel, is plausibly a bug in RPC Dart.
I have a json string looking something like this:
{
"Bar": {
"Pressure":42,
"ServesDrinks":false
},
"Baz":[
{
"Pressure":9001,
"ServesDrinks":true
},
{
"Pressure":0,
"ServesDrinks":false
}
]
}
And I would like to de-serialize this into a class looking something like this:
public class Foo
{
public Bar Bar { get; set; }
/// <summary>
/// Raw serialized (un-de-serialized) json string of the Baz list
/// </summary>
public string Baz { get; set; }
}
Basically not de-serializing the Baz value and instead storing the raw string.
I.e. I want the Baz string value after serialization to be:
"[{\"Pressure\":9001,\"ServesDrinks\":true},{\"Pressure\":0,\"ServesDrinks\":false}]"
I am using Json.Net but have not managed to figure out how to do this.
In the ideal case I would just like to put a magic attribute on my Baz property that retains the raw json.
I have seen a very similar question here:
Partially deserialize with JSON.NET, keeping some fields raw
But I did understand how to go with that JObject approach.
(If it is of any help, I know that the data I want to retain is a list)
Thankful for any help!
Based on the answer to the other question, it seems that the solution in JSON.Net for cases like this is to use the appropriate J* type and use their ToString() and Parse() methods to work with the JSON string.
For JS lists, the appropriate type seems to be JArray, not JObject, which is used for {} style Javascript objects.
Please note that I know little about JSON.Net, and perhaps there is a different solution which would avoid the need to use Parse() and ToString() everywhere. Somebody with a better knowledge of that library might give a better answer.
I have a problem with my custom JSON deserializer.
I use Jackson to map JSON to Java and back. In some cases I need to write my own mapping.
I have an object (filter), which contains a set of another object(metaInfoClass). I try to deserialize the filter with Jackson, but I implemented an own deserializer for the inner object.
The JSON looks like this:
{
"freetext":false,
"cityName":null,
"regionName":null,
"countryName":null,
"maxResults":50,
"minDate":null,
"maxDate":null,
"metaInfoClasses":
[
{
"id":31,
"name":"Energy",
"D_TYPE":"Relevance"
}
],
"sources":[],
"ids":[]
}
My deserializer just works fine, it finds all the fields etc.
The problem is, that somehow (no idea why) the deserializer gets invoked on the rest of the JSON string, so the sources token is getting processed, and so on.
This is very weird, since I don't want to deserialize the big object, but only the inner metaInfoClass.
Even more weird: the CollectionDeserializer class keeps calling my deserializer with the json string even after it is ended. So nothing really happens, but the method gets called.
Any idea?
Thanks a lot!
I was able to find a solution.
I modified the implementation (in the deserialize method) to use to following code:
JsonNode tree = parser.readValueAsTree();
Iterator<Entry<String, JsonNode>> fieldNameIt = tree.getFields();
while (fieldNameIt.hasNext()) {
Entry<String, JsonNode> entry = fieldNameIt.next();
String key = entry.getKey();
String value = entry.getValue().getTextValue();
// ... custom code here
}
So with this approach, it was parsing only the right piece of the code and it's working right now.