Generate only $ref in JSON.NET JsonSchemaGenerator? - json.net

I have a goal of automating converting some of my .NET model classes into Java classes. The current method I am attempting is to use JSON.NET to generate a JSON Schema and then use jsonschema2pojo to produce the Java classes. I have a .NET backend and .NET, Android, and iOS clients. This particular piece is for the Android client to consume JSON RESTful resources from the .NET server.
My specific question is:
Is there a way to have JSON.NET's JsonSchemaGenerator not recurse into nested classes but instead to always specify a $ref? I'm not seeing any options for JsonSchemaGenerator and the JsonProperty attribute doesn't seem to contain anything that would help. I would like to have a schema file for each class and no duplicate definitions of a class when it's nested within another class. jsonschema2pojo seems to work well with a directory full of schema files with each file having the name of the class.

Related

JSON string requested using AJAX from WCF service missing .d and __type attributes

I am using jQuery AJAX to call a WCF service. The service retuns a JSON string that represents an instance of a custom object.
What I'm writing is meant to replace some current functionality in an existing project. I first wrote a demo project to make sure my new code worked without exposing it to the trappings of the exisiting project. The demo project returns a JSON string formated as {"d":{"__type":"MyObject", "Attribute1":"Value"...}.
Once I had the demo working perfectly, I moved the code into the existing project. The JSON now returns a JSON string formatted as {"Attribute1":"Value"...}. The ".d" and "__type" attributes are missing.
I actually make use of the "__type" attribute, so not having it returned is a problem. I've read that the ".d" attribute was a new addition to ASP.NET 2.0, so my initial thought is that the exisiting project is using an earler version. However, in the examples I've seen of this, the "__type" attribute did exist in the earlier ASP.NET versions, and I've also upgrated the project to .NET 4.0 without any resolution.
Where did the attributes go, and how do I get them back?

How do I use a custom JsonConverter with NServiceBus?

I have a class that needs a custom JsonConverter to properly be deserialized. It is a value object (in DDD terminology), and I need to include it in a message being sent with NServiceBus when using Json serialization.
The problem is, since NServiceBus internalizes their copy of Json.Net, I have to use the JsonConverter base class that is included in NSB, but it has been marked "internal" during the merge.
This basically prevents you from hooking any custom serialization code into NSB. Is this by design? Is there a recommended workaround?
If you are having problems with the merged assemblies you can find the core only assemblies on the downloads page on github.

WCF: Should all datacontracts use the same namespace?

I have several service contracts exposed over WCF, which use multiple datacontracts. The service is to be consumed by Adobe Flex. I've run into many problems getting Flex to consume the WSDL produced by the server. I used the procedure outlined here to squeeze the WSDL into a single file (I used FlatWsdl and not WcfExtras, if it matters).
Now I'm getting a bunch of errors when using VS2010's own wsdl.exe tool to extract my metadata. Specifically, I'm getting the following errors:
1) Schema validation warning: Schema item 'complexType' named
'ArrayOfKeyValueOfSomeKeySomeValuep1alXzIb' from namespace
'http://schemas.microsoft.com/2003/10/Serialization/Arrays' is invalid.
Namespace 'http://My/Wcf/Namespace' is not available to be referenced
in this schema.
2) Schema validation warning: Schema item 'complexType' named 'MyComplexType'
from namespace 'http://My/Wcf/Namespace' is invalid. Namespace
'http://schemas.datacontract.org/2004/07/My.Real.Namespace' is not available
to be referenced in this schema.
Now, the Dictionary ("ArrayOfKeyValueOf...") type is apparently defined in an external schema, which is not imported by my WSDL. Please note that SomeKey and SomeValue have a datacontract namespace of My/Wcf/Namespace.
As for MyComplexType, the weird thing is that if I change its Datacontract-namespace to the same namespace used by my services, servers, and bindings, then error (2) goes away. I can't figure out why.
What I'm asking boils down to two questions:
1) How can I add an import directive of an external schema to my WSDL? I saw some solutions on-line that suggested writing a wrapper class around the containers. This is something I would really like to avoid.
2) Do I really have to put all my datacontracts into a single namespace? I don't have any technical objection to doing it, but it's going to be very cumbersome to modify the namespace of all datacontracts. Perhaps there's an automated way of achieving this?
TIA!
So just in case anyone else runs into the same problem with accessing a WCF (BasicHttpBinding) server from a Flex client:
1) No, all DataContracts need not be in the same namespace (but all ServiceContracts, service implementations, and bindings do have to be in the same namespace!).
2) The crux of the problem is that Flex expects each xsd:schema to have an "xsd:import namespace" for all the namespaces it references. This import should be of the form:
<xsd:import namespace="http://schemas.datacontract.org/2004/07/SomeNamespace" />
There doesn't seem to be a way to do this with WCF, since it automatically adds an auto-generated schemaLocation attribute to this tag, which makes Flex try to fetch this schemaLocation. The only workaround appears to be manually manipulating the WSDL at the XML level (e.g. by exposing a REST endpoint for retrieving the metadata).
Thanks to Yaron Naveh in the MSDN WCF forums for his help. The discussion can be found at http://social.msdn.microsoft.com/Forums/en/wcf/thread/b9429e30-e4d5-454f-9fbd-bae39990ff33.

How can I list the properties of an object programmatically in VB6?

A friend of mine is working on a legacy VB6 project. I haven't touched that language in ten years, so I'm pretty rusty. Anyway, is there any kind of reflection API for VB6? Specifically, he needs a way to iterate the properties (and types) of a user-created Class. (In other words, not an external COM object, but an internal "Class Module" as it's called.)
How can this be done?
Jay's answer is the way to go if your project is an ActiveX (ActiveX EXE, DLL, or OCX, as opposed to Standard EXE), and if the classes are public.
However, you mentioned that your friend wants to do this with "internal class modules". I'm not sure if you are referring to private .cls files (classes), or .bas files (modules), but either way, you can only use the TypeLib Information Object Library to reflect on public classes, user-defined types, constants, or enumerations.
You cannot use the library to reflect modules, private classes, or anything else that is declared private.
As a general rule of thumb, you can only use reflection on the things that you can see in the Object Browser when viewing your project's contents from another project. That is, if you compile your project, create a new project, add the first project as a reference, and then view the first project's contents in the Object Browser, anything you can see in the Object Browser can be accessed via the TypeLib Information Object Library. If something is not listed in the Object Browser, then you won't be able to use the TypeLib Information Object Library to reflect it.
VB6 doesn't have any built-in support for run-time reflection or introspection. Using the TypeLib Information Object Library for reflection works for ActiveX VB6 projects because ActiveX projects are compiled in COM components with embedded type libraries, but as mentioned you can only access the data types that are publicly exposed in the compiled component's type library. Using the Object Browser is a quick way to determine what is in the type library because the Object Browser actually inspects the component's type library to populate what you see in the Object Browser, as long as you are viewing the component's contents from a separate project (if you view the component from its own VB6 project, it will display public and private data structures, i.e. everything that is visible in the IDE).
Is this the sort of thing you're looking for:
Visual Basic: Inspect COM Components Using the TypeLib Information Object Library
It's discussed on this thread here on SO: Self Inspection of VB6 UDTs
I've never tried this stuff myself.

Web service is expecting a DataSet object, how can I provide that via ColdFusion or in raw XML?

I need to make a call to a web service written in .NET. The application making the call is written in ColdFusion. One of the parameters the web service expects is a DataSet object. I can't instantiate a .NET DataSet object in ColdFusion, how can I pass the web service something it will accept? I have no problem writing the SOAP request in raw XML, I just don't know what the XML for a DataSet object would look like.
All objects that .NET expects are serialized by Axis and are available to you. Unfortunately ColdFusion does not make it easy to get to.
To get to the stubs you must:
Access the WSDL in any way with coldfusion.
Look in the CF app directory for the stubs. They are in a "subs"
directory, organized by WSDL.like:
c:\ColdFusion8\stubs\WS\WS-21028249\com\foo\bar\
Copy everything from "com" on down into a new directory that exists in
the CF class path. or you can make one like:
c:\ColdFusion8\MyStubs\com\foo\bar\
If you created a new directory add it to the class path. and restart CF services.
Use them like any other java object with or CreateObject()
MyObj = CreateObject("java","com.foo.bar.MyObject");
Your dataset object should be in there somewhere in whatever java format Axis decided it should be. Most likely you're going to need to do almost all of this in cfscript
EDIT FOR QUESTIONS
THe SOAP object will define the object structure and Axis will create methods for manipulating it. Take a look at the Java object that axis creates. Remember that you can use CFDUMP to look at the methods and properties.
Now I HAVE seen .NET objects that Axis gets confused by, like the dreaded non-generic collection that turns into a "ArrayOfAnyType". It's important for .NET developers to use Generics in their services so that Axis can define the arrays properly....if they don't then it sucks and you may not be able to work with it in soap.
but have no fear obi-won...there is another way. You can always interact with .NET web services in a XML/RPC kind of style. It's not automatic, its a lot of hand parsing of XML, it sucks, but sometimes it's the only way to do it. You should be able to get some help from .NET by hitting up the .asmx file without the "?wsdl" on the end. If you do that .NET will generate a bunch of documentation and examples of what the calls and the XML look like. In that case, you can just create the XML and pass it over the wire as specified by using cfhttp. Good Luck!
P.S. I should note also that as far as I know there is no way to mix hand rolled XML with the ColdFusion/Apache Axis objects, there is also no way to model your own object for use with CF/Axis...you must use the stubs or nothing
Could you use JSON?
http://json.org/

Resources