Deserializing Chrome Bookmark JSON Data in C# - c#

In response to a question I asked a few days ago, I'm attempting to stretch myself a little, and do something that I've not really focussed on much before. I've done some searching (both here, and in general), but can't find the answers (or even reasonable hints) to what I want to achieve (though, a few things come close-ish).
Basically, I'm trying to deserialize the data for the Google Chrome bookmarks file using the Json.NET library (though, if there's a better alternative, I'm all for that - the documentation for this library is a little confusing in places). I'm a little confused as to the next step to take, due primarily to being used to PHP's fantastic handling of JSON data (using json_decode()), allowing for a single function call, and then simple associative-array access.
The library (Json.NET) wants me to specify an Object type that it can deserialize the JSON data into, but I'm not really sure how to go about structuring such an Object, given the format of the Bookmarks file itself. The format is something along the lines of:
{
"roots": {
"bookmark_bar": {
"children": [ {
"children": [ {
"date_added": "12880758517186875",
"name": "Example URL",
"type": "url",
"url": "http://example.com"
}, {
"date_added": "12880290253039500",
"name": "Another URL",
"type": "url",
"url": "http://example.org"
} ],
"date_added": "12880772259603750",
"date_modified": "12880772452901500",
"name": "Sample Folder",
"type": "folder"
}, {
"date_added": "12880823826333250",
"name": "Json.NET",
"type": "url",
"url": "http://james.newtonking.com/pages/json-net.aspx";
} ],
"date_added": "0",
"date_modified": "12880823831234250",
"name": "Bookmarks bar",
"type": "folder"
},
"other": {
"children": [ ],
"date_added": "0",
"date_modified": "0",
"name": "Other bookmarks",
"type": "folder"
}
},
"version": 1
}
Now, in PHP, I'd be far more used to doing something along the lines of the following, to get the data I wanted, and ending up with 'Json.NET':
$data['roots']['bookmark_bar']['children'][0]['name'];
I can work out, simply enough, what objects to create to represent the data (something like a root object, then a bookmark list object, and finally an individual bookmark object) - but I'm really not sure as to how to implement them, and then get the library to deserialize into the relevant objects correctly.
Any advice that can be offered would be greatly appreciated.

It is not necessary to declare a type that reflects the json structure:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System;
class Program
{
static void Main(string[] args)
{
string json =
#"
{
""roots"": {
""bookmark_bar"": {
""children"": [ {
""children"": [ {
""date_added"": ""12880758517186875"",
""name"": ""Example URL"",
""type"": ""url"",
""url"": ""http://example.com""
}, {
""date_added"": ""12880290253039500"",
""name"": ""Another URL"",
""type"": ""url"",
""url"": ""http://example.org""
} ],
""date_added"": ""12880772259603750"",
""date_modified"": ""12880772452901500"",
""name"": ""Sample Folder"",
""type"": ""folder""
}, {
""date_added"": ""12880823826333250"",
""name"": ""Json.NET"",
""type"": ""url"",
""url"": ""http://james.newtonking.com/pages/json-net.aspx""
} ],
""date_added"": ""0"",
""date_modified"": ""12880823831234250"",
""name"": ""Bookmarks bar"",
""type"": ""folder""
},
""other"": {
""children"": [ ],
""date_added"": ""0"",
""date_modified"": ""0"",
""name"": ""Other bookmarks"",
""type"": ""folder""
}
},
""version"": 1
}
";
using (StringReader reader = new StringReader(json))
using (JsonReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer serializer = new JsonSerializer();
var o = (JToken)serializer.Deserialize(jsonReader);
var date_added = o["roots"]["bookmark_bar"]["children"][0]["date_added"];
Console.WriteLine(date_added);
}
}

Related

Properly format a message for slack in bot framework

I'd like to send a message with buttons to the slack channel. I'm using the bot framework (c#). I want to use the "blocks" (attachments are deprecated according to the slack api docs). So I composed a sample message in the slack "Bot Kit Builder":
The json for it looks like this:
[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Which pill do you want to take?"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Red",
"emoji": true
},
"value": "red"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Blue",
"emoji": true
},
"value": "blue"
}
]
}
]
As I understand, I have to provide this content in the ChannelData property of the message I sent to the channel:
if (turnContext.Activity.ChannelId == Channels.Slack)
{
message = turnContext.Activity.CreateReply();
message.ChannelData = ChannelDataBuilder.Create("Which pill do you want to take?", "Red", "Blue");
}
The code of the ChannelDataBuilder looks like this:
public static dynamic Create(string text, params string[] choices)
{
var blocks = new List<Block> { new Section { Text = new Text { TextValue = text } } };
var elements = choices.Select(
c => new Button { Text = new Text { TextValue = c, Type = "plain_text" }, Value = c });
blocks.Add(new Actions { Elements = elements.ToArray() });
return JArray.FromObject(blocks, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore });
}
The resulting json of this method looks like this:
{[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Which pill do you want to take?"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Red"
},
"action_id": "9e8ea9fb9267484a9f02b1837f716f69",
"value": "Red"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Blue"
},
"action_id": "34c3d9509fc04e2ea37ed54a70b78486",
"value": "Blue"
}
]
}
]}
So, basically I wonder how I should generate this array of json object using c#. Currently the array is still surrounded by the curly brackets (the list object), but I guess I have to provide an array of json objects.
I've already tried using the JsonConvert class and setting the ChannelData as string. But then nothing appears in the slack channel.
The channelData property allows you to pass a complete Slack message, but your are missing the required top-level properties.
If you want to include blocks, than those have to be defined under the blocks property.
So your JSON need to look more like this (not include the channelData property):
{
"blocks":
[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Which pill do you want to take?"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Red"
},
"action_id": "9e8ea9fb9267484a9f02b1837f716f69",
"value": "Red"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Blue"
},
"action_id": "34c3d9509fc04e2ea37ed54a70b78486",
"value": "Blue"
}
]
}
]
}
See here for the relevant documentation for the botframework.
And here you can see how a message payload for Slack is defined.
Update
As mentioned by #mdrichardson the botframework currently does not support blocks. (See this issue on their github)
So while syntactically correct this solutions does currently not work.
Until botframework supports blocks I would suggest to use secondary attachments.

Autodesk Forge: Download checklist attachment

When I retrieve a checklist instance, I got the following section related to attachment.
{
"type": "instance_item_attachments",
"id": "5a0a2acf-b02a-4b88-86cc-962c3831bdee",
"attributes": {
"name": "6856ad10-6ab0-11e9-9150-9fda3da0626e.png",
"attachmentType": "OSS",
"mimeType": "image/png",
"uploadStatus": "COMPLETED",
"urns": [
{
"urn": "urn:adsk.wipprod:fs.file:vf.gy4mB910SneymU86Gc4O0A?version=1",
"type": "WIP"
},
{
"urn": "urn:adsk.objects:os.object:wip.dm.prod/ede3de59-1b68-485c-82fe-f1f2af3442fe.png",
"type": "OSS"
},
{
"urn": "urn:adsk.checklists.cs.attachment:58b8afcf-d7cd-49ad-aa10-78c50610761b/5a0a2acf-b02a-4b88-86cc-962c3831bdee",
"type": "CHECKLIST"
}
],
"createdAt": "2019-04-29T18:55:51.334Z",
"updatedAt": "2019-04-29T18:55:54.137Z",
"createdBy": "TAKCJQU6HGXW",
"modifiedBy": "TAKCJQU6HGXW",
"permittedActions": [
"canArchive",
"canEdit"
],
"permittedAttributes": [
"mimeType",
"uploadStatus"
]
},
"links": {
"self": "/containers/58b8afcf-d7cd-49ad-aa10-78c50610761b/instance_item_attachments/5a0a2acf-b02a-4b88-86cc-962c3831bdee"
},
"relationships": {
"container": {
"meta": {
"relation": "primary",
"readOnly": false
},
"links": {
"self": "/containers/58b8afcf-d7cd-49ad-aa10-78c50610761b/instance_item_attachments/5a0a2acf-b02a-4b88-86cc-962c3831bdee/relationships/container",
"related": "/containers/58b8afcf-d7cd-49ad-aa10-78c50610761b/instance_item_attachments/5a0a2acf-b02a-4b88-86cc-962c3831bdee/container"
},
"data": {
"type": "containers",
"id": "58b8afcf-d7cd-49ad-aa10-78c50610761b"
}
},
"item": {
"meta": {
"relation": "primary",
"readOnly": false
},
"links": {
"self": "/containers/58b8afcf-d7cd-49ad-aa10-78c50610761b/instance_item_attachments/5a0a2acf-b02a-4b88-86cc-962c3831bdee/relationships/item",
"related": "/containers/58b8afcf-d7cd-49ad-aa10-78c50610761b/instance_item_attachments/5a0a2acf-b02a-4b88-86cc-962c3831bdee/item"
},
"data": null
}
}
}
Now, I want to download this attachment, the provided URN is: wip.dm.prod/ede3de59-1b68-485c-82fe-f1f2af3442fe.png
If I try to access it using the following link, it says not found
developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/b30e3ffe-333b-446c-b834-e2f2141096b4.png
However, if I changed the URL a bit (by adding objects), it works fine.
developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/b30e3ffe-333b-446c-b834-e2f2141096b4.png
Am I doing something wrong here? or this is a bug in the provided urn?
Adding to Adam Nagy reply, you would need to break the URN. From your original question:
urn:adsk.objects:os.object:wip.dm.prod/ede3de59-1b68-485c-82fe-f1f2af3442fe.png
In .NET you can try (using System.Linq):
string bucketKey = urn.Split("/").First().Split(":").Last();
string objectName = urn.Split("/").Last();
Then rebuild as:
string attachemtnUrl = string.Format("{0}/oss/v2/buckets/{1}/objects/{2}", BASE_URL, bucketKey, objectName);
And you'll also need the Authorization header with a valid access token.
The id / urn of an object in OSS (Object Storage Service) contains the bucket name and object name after the "urn:adsk.objects:os.object:" section.
There is a tutorial on downloading a file https://forge.autodesk.com/en/docs/data/v2/tutorials/download-file/
It shows that the reply concerning an item contains both the id and the actual URL of the download link under the storage section:
"storage": {
"data": {
"type": "objects",
"id": "urn:adsk.objects:os.object:wip.dm.prod/977d69b1-43e7-40fa-8ece-6ec4602892f3.rvt"
},
"meta": {
"link": {
"href": "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/977d69b1-43e7-40fa-8ece-6ec4602892f3.rvt"
}
}
}
There you can see the connection between the id and the URL you can use to download the file

NJsonSchema: JSON schemas with nested arrays generate incorrect C# types

I would like to use NJsonSchema to generate C# classes. The problem is that for nested arrays, the type ends up looking like this:
System.Collections.ObjectModel.ObservableCollection<System.Tuple<System.Linq.Enumerable+WhereSelectEnumerableIterator2[NJsonSchema.JsonSchema4,System.String]>>
My test code for generating the classes looks like this:
var schemaAsync = NJsonSchema.JsonSchema4.FromFileAsync(
#"<myPath>/MyFile.json");
var schema = schemaAsync.Result;
var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(schema);
var file = generator.GenerateFile();
System.IO.File.WriteAllText(
#"<myPath>/SomeClass.cs",
file);
I have two JSON schema files, there's the definition file (Def1.json):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"InnerList": {
"description": "Only ever has 2 items",
"type": "array",
"items": [
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
},
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
}
],
"additionalItems": false
},
"OuterList": {
"type": "array",
"items": {
"$ref": "#/definitions/InnerList"
}
},
}
}
And a separate schema that makes use of the definition file:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MySchema",
"type": "object",
"required": [ "OuterList" ],
"properties": {
"OuterList": { "$ref": "Def1.json#/definitions/OuterList" }
},
"additionalProperties": false
}
Am I using the library incorrectly?
This problem has been fixed in the latest version.

Bot framework - Getting the user's answers from a form generated by json schema

I am using the function to BuildJsonForm to define a form using a JSON schema. I generate the JObject with some parameters the bot asks the user during runtime.
An example of the JObject/JSON I send to the function BuildJsonForm is this one:
`
{
"References": [
"Microsoft.Bot.Connector.dll",
"System.dll",
"mscorlib.dll",
"System.Net.Http.dll"
],
"Imports": [
"Microsoft.Bot.Connector.ThumbnailCard",
"Microsoft.Bot.Connector.StateClient",
"System.Net.Mail",
"System",
"System.Text.RegularExpressions",
"System.Net.Http",
"System.Net",
"System.Text"
],
"type": "object",
"required": [
"username",
"password"
],
"Templates": {
"NotUnderstood": {
"Patterns": [
"I do not understand, Please rephrase that"
]
},
"EnumSelectOne": {
"Patterns": [
"Choose one please"
],
"ChoiceStyle": "Auto"
}
},
"properties": {
"username": {
"Prompt": {
"Patterns": [
"Tell me the {&}, please",
"I need you to especify a {&}, please"
]
},
"type": [
"string",
"null"
],
"Templates": {
"NotUnderstood": {
"Patterns": [
"That is not a valid input"
]
}
}
},
"password": {
"Prompt": {
"Patterns": [
"Tell me the {&}, please",
"I need you to especify a {&}, please"
]
},
"type": [
"string",
"null"
],
"Templates": {
"NotUnderstood": {
"Patterns": [
"That is not a valid input"
]
}
}
}
},
"OnCompletion": "await context.PostAsync(\"Thank you!\"); string files = \"\"; context.PrivateConversationData.TryGetValue<string>(\"Files\", out files); [more code...]"
}
`
I need to send to the database the user's answers to the generated JObject/JSON form's questions, but so far, I haven't found a way to do that.
I also tried accessing the BotData with this line context.PrivateConversationData.TryGetValue<string>("Files", out files);, so I could send the user's answers to the database directly from the "OnCompletion" section of the JSON, but still I can't seem to access to the botdata or context on the OnCompletion section.
Is there any other way I can successfully retrieve the user's responses to the JObject/JSON generated form after the user answers the last question in the form?
It seemed that what was causing the trouble in my project were sending this parameters to the function:
GeneratedForm.BuildJsonForm(channel, user, convers);
since I edited the function without those parameters and I stopped getting the exception specified in the question. I will look for the reason these parameters were causing problems but the solution I found in this case was to define the funtction this way:
GeneratedForm.BuildJsonForm();

C# Deserialize Json response for only one property (could't find root) using Newtonsoft.json

I would like to extract only one property from Json response and its in the root element. I tried several attempts but couldn't successful. Please advice.
[
{
"discountAmount": 0,
"discountPercentage": 0,
"originalPrice": null,
"manufacturerProductCode": "9H.JA677.25E",
"bulletPoints": [
"Full HD (1920x1080)",
"Kuvakoko 26\"-300\"",
"Suuri valovoima, 4000 Lm"
],
"href": "/fi/product/16592/dxrdm/BenQ-SH915-Full-HD-DLP-asennettava-projektori",
"name": "BenQ SH915 Full HD DLP ā€asennettava projektori",
"images": [
{
"host": "cdn-b.verkkokauppa.com",
"path": "/images/14/2_195368-2500x1488.jpeg"
},
{
"host": "cdn-a.verkkokauppa.com",
"path": "/images/36/2_195368-3000x1790.jpeg"
},
{
"host": "cdn-d.verkkokauppa.com",
"path": "/images/51/2_195368-3000x1153.jpeg"
},
{
"host": "cdn-b.verkkokauppa.com",
"path": "/images/81/2_195368-3000x1425.jpeg"
},
{
"host": "cdn-a.verkkokauppa.com",
"path": "/images/96/2_195368-3000x1449.jpeg"
},
{
"host": "cdn-d.verkkokauppa.com",
"path": "/images/11/2_195368-3000x2200.jpeg"
},
{
"host": "cdn-c.verkkokauppa.com",
"path": "/images/26/2_195368-3000x1161.jpeg"
}
],
This is the Json response and I would need (manufacturerProductCode) from this response.
Dynamic object can solve this problem easily. Or you can try Linq to Json.
dynamic obj = JObject.Parse(json);
Console.WriteLine((string)obj.manufacturerProductCode);

Categories

Resources