Restkit: Coerced object mapping result containing 10 objects into singular result. - json

If I'm getting back an array of objects in JSON, and I've set a mapping for those objects, why does RestKit only return me one object instead of an array?
RestKit gives this output in the log:
Coerced object mapping result containing 10 objects into singular result.

This depends on the Method you invoke on the RKObjectMappingResult.
...
yourMapping = [RKObjectMapping mappingForClass:[YourExchangeObject class]];
[yourMapping mapKeyPath:#"attribute" toAttribute:#"attribute"];
...
RKObjectMappingProvider *provider = [RKObjectMappingProvider new];
[provider setMapping:yourMapping forKeyPath:#""];
RKObjectMapper *mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:provider];
RKObjectMappingResult *mappingResult = [mapper performMapping];
so now if you call
NSObject *object = [mappingResult asObject];
you'll get the "Coerced object mapping result containing ..." warning and only one result will be deliverd
call instead
NSArray *array = [mappingResult asCollection];
and it should work

Related

How do I serialize and deserialize a tuple in Rust using Serde?

I have a tuple consisting of an String and a Uuid that I serialize using serde_json:
let log_and_id = (String::from("Test string"), test_id);
let log_and_id_serialized = serde_json::to_string(&log_and_id)
.expect("Serialization failed");
//After serialization (debug print): "[\"Test string\",\"32a8e12d-69d2-421d-a52e-1ee76cc03ed5\"]"
Then I transfer this serialized value over the network and receive a BytesMut (serialized_tuple) on the other end, which I try to deserialize:
//Bytesmut value (debug print): b"\"[\\\"Test string\\\",\\\"32a8e12d-69d2-421d-a52e-1ee76cc03ed5\\\"]\""
let (log, operation_state_id) = serde_json::from_slice::<(String, Uuid)>(&serialized_tuple)?;
But I get the following error:
ERROR actix_http::response] Internal Server Error: SerdeError(Error("invalid type: string \"[\\\"Test string\\\",\\\"32a8e12d-69d2-421d-a52e-1ee76cc03ed5\\\"]\", expected a tuple of size 2", line: 1, column: 68))
(De)serializing single objects this way used to work in other parts of this code, so what could cause it to fail when used with tuples?
You don't have a serialized tuple, but a serialized serialized tuple.
I mean the serialization of the tuple, which was a JSON string, was again serialized.
You can check this with this code (playground):
let serialized_tuple = b"\"[\\\"Test string\\\",\\\"32a8e12d-69d2-421d-a52e-1ee76cc03ed5\\\"]\"";
let serialized_tuple: String = serde_json::from_slice(serialized_tuple).unwrap();
let (log, operation_state_id) = serde_json::from_slice::<(String, String)>(serialized_tuple.as_bytes()).unwrap();
which produces the desired tuple.
Of course, rather than deserializing twice, you should remove the unnecessary serialization from your application (it's not in the code you've shown).

Deserializing a JSON value from an API - curly vs. square brackets

I am working to integrate some weather info from an API where I get a return value like this ...
{"coord":{"lon":-85.6,"lat":43.05},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":277.33,"pressure":1010,"humidity":65,"temp_min":276.15,"temp_max":279.15},"visibility":16093,"wind":{"speed":5.7,"deg":100,"gust":8.2},"clouds":{"all":75},"dt":1524076680,"sys":{"type":1,"id":1410,"message":0.0043,"country":"US","sunrise":1524048860,"sunset":1524097768},"id":420018526,"name":"Grand Rapids","cod":200}
I have created a built_value and built_collection object to deserialize into. If I build the object "by hand" and serialize it, and JSON encode it ...
var w = new Welcome((b) => b
..id = 420018526
..name = "Grand Rapids"
..weather.add(new Weather((w)=>w
..id = 803
..main = "Clouds"
..description = "broken clouds"
..icon = "04d"
))
..cod = 200
..coord.lat = 43.05
..coord.lon= -85.6
..base = 'stations'
..main.temp = 277.33
..main.pressure = 1010
..main.humidity = 65
..main.temp_min = 276.15
..main.temp_max = 279.15
..visibility = 16093
..wind.speed = 5.7
..wind.deg = 100
..wind.gust = 8.2
..clouds.all = 75
..dt = 1524076680
..sys.type = 1
..sys.id = 1410
..sys.message = 0.0043
..sys.country = "US"
..sys.sunrise = 1524048860
..sys.sunset = 1524097768
);
var ws = serializers.serialize(w);
var wsj = JSON.encode(ws);
I get something like this ...
["Welcome","coord",["lon",-85.6,"lat",43.05],"weather",[["id",803,"main","Clouds","description","broken clouds","icon","04d"]],"base","stations","main",["temp",277.33,"pressure",1010,"humidity",65,"temp_min",276.15,"temp_max",279.15],"visibility",16093,"wind",["speed",5.7,"deg",100,"gust",8.2],"clouds",["all",75],"dt",1524076680,"sys",["type",1,"id",1410,"message",0.0043,"country","US","sunrise",1524048860,"sunset",1524097768],"id",420018526,"name","Grand Rapids","cod",200]
... which looks amazingly similar, but you'll notice that the dart:convert JSON libraries seem to prefer square brackets [ over curly ones {
When I try to deserialze the curly brackets wsjdA = JSON.decode(wJson); I get an error
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Welcome' of 'wsjdA' where
_InternalLinkedHashMap is from dart:collection
String is from dart:core
Welcome is from package:ex_models/src/weather.dart
But if I do this goofy thing (to parse out the square brackets for curly and add the first string "Welcome" ...
var wJsonB= '["Welcome",' + wJson
.replaceAll('{', '[')
.replaceAll('}', ']')
.replaceAll(':', ',')
.substring(1);
... it works fine
Am I missing something - is there an easier way to do the JSON/Object deserialization in dart. I'd really like to be able to parse a valid JSON API response into a dart object, in a standard way, without the manual parsing.
I think this may have worked if it weren't for the list of Weather objects within the Root/Welcome object. Because this object contains a list of objects, this is confusing things a bit? This is a BuiltList in my object.
I am already using a built_value object with built_collections and using the dart:convert libraries. Wondering if there is something I am missing?
[] is for lists/arrays, {} is for maps. With built_value you can use properties of type BuiltList and BuiltMap.
JSON.decode(wJson); doesn't return instances of classes, it just makes lists, maps, and separate primitive values out of a JSON string.
built_value has it's own serialization from what JSON.decode(...) returns to concrete instances of classes.
built_value by default has a reduced JSON format that won't be compatible with your JSON. For that you need to register the StandardJsonPlugin (see also https://github.com/google/built_value.dart/issues/171)
For details about serialization with built_value see https://medium.com/dartlang/darts-built-value-for-serialization-f5db9d0f4159.

Is there a XPath with schema equivilent for JSON? [duplicate]

I have JSON as a string and a JSONPath as a string. I'd like to query the JSON with the JSON path, getting the resulting JSON as a string.
I gather that Jayway's json-path is the standard. The online API, however, doesn't have have much relation to the actual library you get from Maven. GrepCode's version roughly matches up though.
It seems like I ought to be able to do:
String originalJson; //these are initialized to actual data
String jsonPath;
String queriedJson = JsonPath.<String>read(originalJson, jsonPath);
The problem is that read returns whatever it feels most appropriate based on what the JSONPath actually finds (e.g. a List<Object>, String, double, etc.), thus my code throws an exception for certain queries. It seems pretty reasonable to assume that there'd be some way to query JSON and get JSON back; any suggestions?
Java JsonPath API found at jayway JsonPath might have changed a little since all the above answers/comments. Documentation too. Just follow the above link and read that README.md, it contains some very clear usage documentation IMO.
Basically, as of current latest version 2.2.0 of the library, there are a few different ways of achieving what's been requested here, such as:
Pattern:
--------
String json = "{...your JSON here...}";
String jsonPathExpression = "$...your jsonPath expression here...";
J requestedClass = JsonPath.parse(json).read(jsonPathExpression, YouRequestedClass.class);
Example:
--------
// For better readability: {"store": { "books": [ {"author": "Stephen King", "title": "IT"}, {"author": "Agatha Christie", "title": "The ABC Murders"} ] } }
String json = "{\"store\": { \"books\": [ {\"author\": \"Stephen King\", \"title\": \"IT\"}, {\"author\": \"Agatha Christie\", \"title\": \"The ABC Murders\"} ] } }";
String jsonPathExpression = "$.store.books[?(#.title=='IT')]";
JsonNode jsonNode = JsonPath.parse(json).read(jsonPathExpression, JsonNode.class);
And for reference, calling 'JsonPath.parse(..)' will return an object of class 'JsonContent' implementing some interfaces such as 'ReadContext', which contains several different 'read(..)' operations, such as the one demonstrated above:
/**
* Reads the given path from this context
*
* #param path path to apply
* #param type expected return type (will try to map)
* #param <T>
* #return result
*/
<T> T read(JsonPath path, Class<T> type);
Hope this help anyone.
There definitely exists a way to query Json and get Json back using JsonPath.
See example below:
String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"Ghaziabad\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"Y\", \"is_oda\": \"N\", \"sort_code\": \"GB\", \"state_code\": \"UP\"}}]}";
String jsonExp = "$.delivery_codes";
JsonNode pincodes = JsonPath.read(jsonExp, jsonString, JsonNode.class);
System.out.println("pincodesJson : "+pincodes);
The output of the above will be inner Json.
[{"postal_code":{"district":"Ghaziabad","pin":201001,"pre_paid":"Y","cash":"Y","pickup":"Y","repl":"N","cod":"Y","is_oda":"N","sort_code":"GB","state_code":"UP"}}]
Now each individual name/value pairs can be parsed by iterating the List (JsonNode) we got above.
for(int i = 0; i< pincodes.size();i++){
JsonNode node = pincodes.get(i);
String pin = JsonPath.read("$.postal_code.pin", node, String.class);
String district = JsonPath.read("$.postal_code.district", node, String.class);
System.out.println("pin :: " + pin + " district :: " + district );
}
The output will be:
pin :: 201001 district :: Ghaziabad
Depending upon the Json you are trying to parse, you can decide whether to fetch a List or just a single String/Long value.
Hope it helps in solving your problem.
For those of you wondering why some of these years-old answers aren't working, you can learn a lot from the test cases.
As of September 2018, here's how you can get Jackson JsonNode results:
Configuration jacksonConfig = Configuration.builder()
.mappingProvider( new JacksonMappingProvider() )
.jsonProvider( new JacksonJsonProvider() )
.build();
JsonNode node = JsonPath.using( jacksonConfig ).parse(jsonString);
//If you have a json object already no need to initiate the jsonObject
JSONObject jsonObject = new JSONObject();
String jsonString = jsonObject.toString();
String path = "$.rootObject.childObject"
//Only returning the child object
JSONObject j = JsonPath.read(jsonString, path);
//Returning the array of string type from the child object. E.g
//{"root": "child":[x, y, z]}
List<String> values = sonPath.read(jsonString, path);
Check out the jpath API. It's xpath equivalent for JSON Data. You can read data by providing the jpath which will traverse the JSON data and return the requested value.
This Java class is the implementation as well as it has example codes on how to call the APIs.
https://github.com/satyapaul/jpath/blob/master/JSONDataReader.java
Readme -
https://github.com/satyapaul/jpath/blob/master/README.md

POCO::PostgreSQL retrieve JSON datatype

What is the best way to retrieve data from JSON field in PostgreSQL using POCO framework?
The only way I see is:
Poco::Data::RecordSet rs(session, sql);
rs.moveFirst();
string value = rs[0].convert<std::string>(); // get JSON as string
Poco::JSON::Parser parser;
parser.parse(value);
Poco::Dynamic::Var result = parser.result();
// now we can extract Object, Array and so on
Direct extract
Poco::JSON::Object object = os[0].extract<Poco::JSON::Object>()
throws Can not convert [ERRFMT] to [ERRFMT] exception.
Any better solution?
Var result = parser.parse(value);
Object::Ptr object = result.extract<Object::Ptr>();

RestKit not inverse mapping date

Restkit is mapping my date just fine to an object, but when trying to do an inverse mapping back to json i'm getting this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Invalid type in JSON write (__NSDate)'
Obviously this is an issue with the json parser for iOS, but I figured the inverse should be handled by RestKit as well. Anyone else experience this or have a solution?
In logging restkit I get this message right before:
D restkit.object_mapping:RKMappingOperation.m:920 Finished mapping operation successfully...
**EDIT:
Mapping example:
// Define media mapping
RKObjectMapping *mediaMapping = [Media getObjectMapping];
// Define Post mapping
RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[self class]];
[postMapping addAttributeMappingsFromDictionary:#{
#"post_id": #"postID",
#"userID": #"userID",
#"title": #"title",
#"favorite": #"favorite",
#"created": #"created",
#"modified": #"modified"}];
// Define relationship in the mapping between Media and Post
[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"media"
toKeyPath:#"media"
withMapping:mediaMapping]];
Discriptor example:
RKObjectMapping *entityMapping = [object getObjectMapping];
RKResponseDescriptor *successDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping
method:RKRequestMethodPOST
pathPattern:request.URL.relativePath
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
**UPDATE2:
Right from the dictionary I break point and inspected the offending value in the destination object.
[11] = #"created" : 2014-04-21 11:04:43 PDT
key __NSCFConstantString * #"created" 0x002d3818
value __NSDate * 2014-04-21 11:04:43 PDT 0x155bab10
I can see the issue, it appears to be passing the dictionary with the NSDate to [NSJSONSerialization dataWithJSONObject: options: error:] via this method, [RKSerialization dataFromObject: error;
Should RestKit not be converting the NSDate using the RKValueTransformer I have defined before passing to NSJSONSerialization?
Open issue at github can be found here.