Read DropBox info.json file with .NetJSON - vb.net

Any ideas, I don't understand the Object I need to define. I am coding in VB but C# is OK. The file is one line but seems to have some nesting with Personal and Business.
{
"personal": {
"path": "C:\\Users\\Paul\\Dropbox (Personal)",
"host": 4897400149,
"is_team": false,
"subscription_type": "Basic"
},
"business": {
"path": "C:\\Users\\Paul\\Dropbox (Y2016)",
"host": 4897401185,
"is_team": true,
"subscription_type": "Business"
}
}

You can define your classes as follows:
Public Class DropBoxFolderPath
Public Property path As String
Public Property host As Long
Public Property is_team As Boolean
Public Property subscription_type As String
End Class
Public Class DropBoxFolderPaths
Public Property personal As DropBoxFolderPath
Public Property business As DropBoxFolderPath
End Class
Then deserialize as follows:
Dim dropBoxFolders = JsonConvert.DeserializeObject(Of DropBoxFolderPaths)(jsonString)
Example fiddle. Related documentation: How can I programmatically find the Dropbox folder paths?.

Related

How to parse this json with newtonsoft?

{
"boxes": [
{
"hash": "51532859",
"owner": "9",
"id": "3868",
"isDisabled": false
"innerbox": {
"color": "aaaa",
"size": "bbbb"
}
},
{
"hash": "cccc",
"owner": "9",
"id": "3868",
"isDisabled": false
"innerbox": {
"color": "cccc",
"size": "dddd"
}
}
],
"meta": {
"currencies": {
"USD": {
"total": "2",
"value": "123456",
"currency": "USD",
"isDisabled": false
}
},
"total": 2
}
}
Above is the JSON (I've cut it down and replaced some info, but its the same format) that I retrieve from a web server. I am trying to use NewtonSoft JSON to Deserialize it but I am getting an error "because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."
Here is my class:
Public Class TheBox
Public Property boxes() As Boxes
Public Property meta As Meta
Public Property usd As USD
Public Property innerbox As InnerBox
End Class
Public Class Meta
Public Property currencies As Currencies
Public Property total As Integer
End Class
Public Class Currencies
Public Property USD As USD
End Class
Public Class USD
Public Property total As String
Public Property value As String
Public Property currency As String
Public Property isDisabled As Boolean
End Class
Public Class Boxes
Public Property hash As String
Public Property owner As String
Public Property id As String
Public Property isDisabled As Boolean
End Class
Public Class InnerBox
Public Property color As String
Public Property size As String
End Class
This is how I am trying to Deserialize it:
Dim obj = JsonConvert.DeserializeObject(Of TheBox)(Json)
Any help is appreciated, thank you
Your current json as posted looks to be "bad" or malformed.
So, this:
"isDisabled": false
"innerbox": {
"color": "aaaa",
"size": "bbbb"
}
There is a comma (",") missing after the false.
so, it needs to be:
"isDisabled": false,
"innerbox": {
"color": "aaaa",
"size": "bbbb"
}
Same for the one further down.
Once you fix those two issues, then this json will and should parse just fine.
You can have Visual Studio create the classes for you like this:
Create a blank new class, and then ctrl-a and del key to empty out. Then this:
And now VS spits out this:
Public Class Rootobject
Public Property boxes() As Box
Public Property meta As Meta
End Class
Public Class Meta
Public Property currencies As Currencies
Public Property total As Integer
End Class
Public Class Currencies
Public Property USD As USD
End Class
Public Class USD
Public Property total As String
Public Property value As String
Public Property currency As String
Public Property isDisabled As Boolean
End Class
Public Class Box
Public Property hash As String
Public Property owner As String
Public Property id As String
Public Property isDisabled As Boolean
Public Property innerbox As Innerbox
End Class
Public Class Innerbox
Public Property color As String
Public Property size As String
End Class
So, the top most root item here is Root object - you can re-name that if you wish.
I not test above with the above data sample (first fix it), but I'm going for a coffee - if I have time, I will give your data a try with above code later.

What is an efficient way to generate vb classes from json schemas?

Is there any way to generate vb classes from jsonschema files like we can generate classes from wsdls and xsds using wsdl.exe in one go.
I don't want to use Edit > Paste special > paste JSON as class feature of Visual Studio because I tried for one file and it did not give me the result I am expecting and also there are about 15 schema files so want a generic way.
On using Edit > Paste special > paste JSON as class feature of Visual Studio,
The schema have is:
{
"title": "MyObject",
"type": "object",
"properties": {
"description": {
"type": "string"
},
"name": {
"type": "string"
},
"id": {
"type": "string"
}
},
"required": [ "id", "description", "name" ]
}
The generated classes:
Public Class Rootobject
Public Property title As String
Public Property type As String
Public Property properties As Properties
Public Property required() As String
End Class
Public Class Properties
Public Property description As Description
Public Property name As Name
Public Property id As Id
End Class
Public Class Description
Public Property type As String
End Class
Public Class Name
Public Property type As String
End Class
Public Class Id
Public Property type As String
End Class

Activex component cant create object error, cannot find cause [duplicate]

Solved, see comments!
I have a simple .NET DLL written in c#.
In asp-classic or VB.NET i can create the object and call a member function in the DLL without any problem.
But, and this is my stumbling point, i can't access class properties.
Here's the sample code:
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComEvents))]
public class Com : IComInterface
{
public string MyProperty{ get; set; } // <-- NOT ACCESSIBLE
public void MyFunction() // <-- ACCESSIBLE
{
}
}
Here's the server-side script:
Set com = Server.CreateObject("ns.Com") // WORKS
com.MyProperty = "abc" // GIVES ERROR
com.MyFunction // WORKS
I get the following error-message:
Microsoft VBScript Runtime Error "800a01b6'
Object Doesn't Support This Property or Method: MyProperty
Can anybody tell me, why i can call the function 'MyFunciton', but if i want to set the property-value, i get the error above?
Properties must be included in the interface definition to make them visible to COM.
Example:
[Guid("... some GUID ...")]
[ComVisible(true)]
public interface MyClassInterface
{
string MyProperty { get; set; }
bool MyMethod();
}

How to get JacksonXML to de-serialize embedded objects

I am writing a REST interface using Jersey and have configured it to use JacksonXML to do the JSON<->POJO conversions. I have a set of classes like this:
#JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonPo {
public Long id;
public String type;
public Boolean committed;
public String owner;
// ...
Which is referenced in my main class like this:
#JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonPoReq {
public Long id;
public String pathstring;
public int firstrecord;
public JsonPo obj;
// ...
And the REST resource of course looks like this:
#POST
#Path("path")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response byPath(JsonPoReq req) {
// ...
The serialization (POJO->JSON) works fine. The properties in the JsonPo object get serialized in a JSON object as you would expect.
The reverse doesn't work. Even if I feed the serialized string back. What happens is the obj property is always set to null.
EDIT: As requested the following is what the JSON string looks like:
{
"id" : 2,
"pathstring" : "/a/b/c",
"firstrecord" : 0,
"obj" : {
"id" : 2,
"type" : "ProtoObject",
"committed" : false,
"owner" : "admin"
}
}
I have been going through the JacksonXML Annotations to see how to fix this but I haven't been able to come up with anything. Is there some other step I am supposed to take?
UPDATE: Problem was not real. Some application code was being invoked that I didn't notice was clearing the object.

JAXB -EclipseLink MOXy Object type not getting marshalled correctly

public class ResposneMessage {
private int status;
private String code = "";
private String message = "";
private Object data;
}
The "DictType" is not marshaled:
{
"code": "",
"data": "com.testapp.model.DictType#7c9a897 com.testapp.model.DictType#43581423 com.testapp.model.DictType#217adb02 com.testapp.model.DictType#6ff992bb com.testapp.model.DictType#253e12c3 com.testapp.model.DictType#2644f34b com.testapp.model.DictType#51919e4a com.testapp.model.DictType#72deb289 com.testapp.model.DictType#27231e1b com.testapp.model.DictType#26fc6f1f com.testapp.model.DictType#7b42c644 com.testapp.model.DictType#7c8f695f com.testapp.model.DictType#43637313",
"message": "",
"status": 200
}
Default is not marshaling the Object type.
An Object can not be marshalled to any meaningful JSON representation because the library responsible for marshalling the object uses reflection to do so. It seems to only look at the top-most class in the hierarchy you defined and marshalls whatever it finds in there.
Since there is nothing for Object, you just get its .toString() representation.
FWIW: The same happens when you try to marshall an interface or (abstract) superclass. You only see the properties the interface/superclass itself defines, in your marshalled output - regardless of what the descendant classes declare/define.
Take this example:
public class SuperClass {
}
public class OtherClass extends SuperClass {
public String someProperty = "test";
}
public class MarshallMe {
public SuperClass classTest = new OtherClass();
}
This would marshall to just "classTest":{} because SuperClass doesn't have any properties of its own.