JSON and VB.net - vb.net

I've got a small utility I use to communicate with Yahoo's APIs. Their servers return all the responses in JSON.
Is there an easy way to parse this into its base form when you don't necessarily know what will be in the response?
{
"sessionId": "A.bpAsPs3RPYF0nUuAnCtuEUJMOmDDHbjZG5",
"primaryLoginId": "prometheussoft",
"displayInfo": {
"avatarPreference": "2",
"checksum": "-1484747745"
},
"server": "rcore1.messenger.yahooapis.com",
"notifyServer": "rproxy1.messenger.yahooapis.com",
"constants": {
"presenceSubscriptionsMaxPerRequest": 60
}
}

you can deserialize it to a dictionary with string parsing, any values that are objects can be done the same way with a nested dictionary.
However to get typing on the values you will need to at least know what is expected for each property

Related

How to use array type for expect response body in RESTinstance for Robot Framework

I am writing Robot tests for API endpoint which returns a response body of an array of objects. I am using RESTinstance to validate the response body.
I have the JSON schema for the individual object.
How does one validate an array of that JSON schema?
I tried these but it didn't work:
Expect Response Body Array ${schema}/alert.json
Expect Response Body [${schema}/alert.json]
I then created a new schema alerts.json which simply is an array of the object schema:
{
"type": "array",
"items": {
"$ref": "file:../schema/alert.json"
}
}
and tried
Expect Response Body [${schema}/alerts.json]
This threw a RefResolutionError for a $ref contained in the alert.json object.
None of these solutions worked
Is there a standard way to achieve this using Robot and REST?
Thanks

ServiceStack Deserialize Json (with types) to List of specific type

I have this json:
{
"$type": "System.Collections.Generic.List<MyType>",
"$values": [
{
"$type": "MyType",
"o": 7.54,
"t": 1619002800000,
"n": 3
},
{
"$type": "MyType",
"o": 7.53,
"t": 1619005140000,
"n": 3
}
]
}
I want to deserialize it back into a List<MyType>. I thought there would be an easy way to do that some thing like this:
var myList = json.FromJson<MyType>();
but that doesn't work.
I have figured out a way to accomplish my goal but it's a bit messy so I was wondering if there's a better way that I'm not aware of. Here's the messy way I came up with:
var myListOfObject = (List<object>)((Dictionary<string, object>)JSON.parse(json))["$values"];
var myTypes = myListOfObject.ConvertAll(x => JSON.stringify(x).FromJson<MyType>());
I'm not necessarily looking for fewer lines of code because 2 isn't anything to complain about. I'm just hoping there is a way that doesn't require all the casting and parsing and rather can accept the json as is and get it back to the type it came from. Maybe there's even a parameter I can set to tell it to validate types during the deserialization since the full type names are in the json.
You should use the same serializer you used to serialize the payload to deserialize it. ServiceStack.Text uses __type to embed its type information, in a different schema so you wont be able to use ServiceStack.Text to automatically deserialize it into the embedded type.
This likely used JSON.NET which you should use instead to deserialize it, otherwise yeah you can use ServiceStack's JS Utils to deserialize arbitrary JSON as you're doing.

VB.NET Processing Json from GitLab

I'm using the API of GitLab in VB.Net.
To request groups, I'm using GET /groups.
GitLab returns a JSON string like this:
[
{
"id":5,
"web_url":"https://XXXXX/groups/AAAA",
"name":"AAAA",
"path":"AAAA",
"description":"blabla",
"visibility":"private",
"share_with_group_lock":false,
"require_two_factor_authentication":false,
"two_factor_grace_period":48,
"project_creation_level":"developer",
},
{
"id":8,
"web_url":"https://XXXXX/groups/BBBBBB",
"name":"BBBBBB",
"path":"BBBBBB",
"description":"",
"visibility":"private",
"share_with_group_lock":false,
"require_two_factor_authentication":false,
"two_factor_grace_period":48,
"parent_id":null,
"ldap_cn":null,
"ldap_access":null
},
etc ...
]
It's quite complicated to parse it with Newtonsoft.Json so I would like first to convert it to an array of Dictionary.
Then, I will be able to loop through the array and get myrow("id") for instance.
I couldn't find how to do this, could you help me please?
String (list of Dictionary) -> List (Dictionary)

Get settings from appsettings.json using the percentage syntax

If you want a ServiceBusTrigger to use a setting from de appsettings.json, you can do this like this:
[ServiceBusTrigger("%CommandQueue:QueueName%")]
But what if I have an array of queues in my appsettings and I want to point a one of those.
{
"Queues": [
{
"QueueName": "ThisOne",
"EndPoint": "TheEndMyFriend"
}
]}
I want to specify something like this:
[ServiceBusTrigger("%CommandQueue:Queues{QueueName=ThisOne}:EndPoint%")]
Is this possible?
No. That's not possible. As it currently stands, the best you can do is an index, since this is an array. In other words, if you knew that the one you wanted to use from the array was the second item, for example, then you could use %CommandQueue:Queues:1:Endpoint%. However, that's pretty fragile, as if you change the order, insert a new queue before it, etc., then your code would break.
If you changed the format of your config to an object of objects:
"Queues": {
"ThisOne": {
"EndPoint": "TheEndMyFriend"
}
}
Then, you could use %CommandQueue:Queues:ThisOne:EndPoint%, which would be more robust.

Is RestKit the only framework that has JSON to Objective-C objects mapping?

I am looking for a library or framework that does JSON to Objective-C relational object mapping.
i.e. I need to map JSON containing objects, array of objects and dictionaries of objects to my custom objects.
something like:
DataObject {
"user" : {
"name":"Peter",
"id":1234
}
"place": "UK"
"job": {
"title" : "CTO",
"salary" : 1234567
}
"employess": [
{
"name":"Carlton",
"id":1235
},
{
"name":"Hugo",
"id":12346
}]
}
So there is a DataObject a UserObject and an employees array consisting of UserObjects.
I would like for the mapping from the JSON to my DataObject to happen "automatically", of course meant as I would like to describe the objects and there relations in the Object class and have the mapping done from this, instead of manually mapping each nested object.
(First level native objective-c properties are easily done with setValue:forKey and other KVO methods, but from there on it gets complicated).
I have been testing out RestKit but it seems there is no way to pick and choose which functionality you need from that framework, it is either all of it or none of it, and I do find it does too much for my needs.
Are anyone familiar with a library etc. out there that can do this?
Thank you in advance.
To map JSON to Objective-C objects, I have tried RestKit. I used it a while ago, so my criticisms might not apply anymore.
What I found out: nice idea. Not too complicated to plug-in. If it works, great for you. But if not, or if you need to push it a bit further, good luck to debug.
I regret the time I invested in it.
I am only looking for JSON to Objective-C objects, not the other way around.
I found a link on SO to JTObjectMapping - but can't find the original answer. Seems more lightweight and close to what I was searching, but I did not had the opportunity to try it.
An other similar class: jastor.
I prefer the approach of this two classes over RestKit, as it only takes care of one job, whereas RestKit tried to handle everything.
What you have posted above isn't valid JSON. If you made it valid JSON what you want to do is impossible without a specific schema, eg.
{
"DataObject": {
"user": {
"name": "Peter",
"id": 1234
},
"place": "UK",
"job": {
"title": "CTO",
"salary": 1234567
}
}
}
Is Dataobject a dictionary or an Object? What about User or Job? What is User is an instance of NSUser and job is an NSDictionary?
On the other hand, if you have a known schema:-
[
{
"class": "DataObject",
"properties": {
"user": {
"class": "User",
"properties": {
"name": "Peter",
"id": 1234
}
},
"place": "UK",
"job": {
"title": "CTO",
"salary": 1234567
}
}
}
]
you don't need a framework as it is trivial to map to your objects yourself once you have valid JSON. Pseudocode:-
createWithDict(dict) {
var newOb = create new(dict["class"]);
dict.properties.each( val, key ) {
if(val is dictionary && val.hasproperty("class"))
val = createWithDict(val)
newOb[key] = val
}
return newOb;
}