7

I've started using Newtonsoft.Json.Schema.JsonSchemaGenerator along with various property attributes over in my C# code to help keep my client script DRY. What I'd like to do is create a default initialized object client-side based on the schema from the server. This would be useful for, say, when the user clicks 'New Foo' to add a new entry into a table.

Obviously I can just code it up to iterate the .Properties and build up the new object, which is what I'm doing at the moment. However I'd prefer to avoid reinventing any wheels.

Are there any JS libraries for working with JSON schema that will do this, among other nifty things I've yet to realize I need?


1/29/2013 UPDATE

Some people have attempted to answer my question and have been off base, and as a result have received some negative feedback from the SO community. So let me attempt to clarify things. Here is the challenge:

  • In JS client script, you have an object that represents the JSON Schema of another object. Let's say, this came from the server via JSON.NET and is the representation of a C# class.

  • Now, in the JS client script, create one of these objects based upon the JSON Schema. Each field/property in the object must be default initialized according to the schema, including all contained objects!

  • BONUS: Bind this new object to the UI using MVVM (eg Knockout). Change some of the fields in response to user input.

  • Send this new object to the server. The server-side code will add it to a collection, database table, whatever. (Yes, the object will be sent as JSON using Ajax -- we can assume that)

  • No duplication! The only place where the class is defined is in the server-side code (C# in my example). This includes all metadata such as default values, description text, valid ranges, etc.

BrandonLWhite
  • 1,866
  • 1
  • 23
  • 26

2 Answers2

1

Yes there is (I tried it with NodeJS):

JSON Schema defaults

Link updated.

damorin
  • 1,389
  • 1
  • 12
  • 15
-1

i think...you have to use two way binding with your HTML code...so, once your client side change you will get on your costume js file.

check here for knockout js.

Knock Out JS Link

and on C# code use : $("#urlhidden").val() OR Document.GetElemenyByID("#urlhidden").val().

here you will get array/list or textbox value

Use json with Ko

create new viewmodel for knockout js which you will get the idea about on above link.

and create a json call like:

 self.LoadMAS_Client = function () {

        try {



            var params = { "clientID": ClientId };

            $.ajax({
                type: "POST",
                url: "http://" + ServerString + "/Services/LogisticsAppSuite-Services-Web-Services-MasClientService.svc/Json/GetAllLevelSubClients",
                contentType: 'application/json',

                data: JSON.stringify(params),
                dataType: 'json',
                async: false,
                cache: false,
                success: function (response) {


              // in response u will get the data.and use as per your requirement.

                  eg.   self.SelectedClient(response.your value);





                },
                error: function (ErrorResponse) {


                }

            });
        }
        catch (error) {



        }
    };

================================New Update ========================================== i think..one way you can do...get data on xml format at C# code and covert into json string...check below code // To convert an XML node contained in string xml into a JSON string

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);

// To convert JSON text contained in string json into an XML node
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);  
Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56
  • How does KO use JSON schemata? – Bergi Jan 29 '13 at 11:39
  • Thanks for answering Jignesh, but your answer pertains to using MVVM, which is certainly a Good Thing, however it doesn't particularly address my question. Indeed though I am using Knockout in my web apps! I love it! – BrandonLWhite Jan 29 '13 at 14:08
  • i think..one way you can do...get data on xml format at C# code and covert into json string... – Jignesh.Raj Jan 30 '13 at 05:03