Validate JSON against JSON Schema C#

I think that you just need to add ‘additionalProperties’: false to your schema. This will stop unknown properties being provided. So now your results will be:- True, False, False test code…. void Main() { var schema = JsonSchema.Parse( @”{ ‘type’: ‘object’, ‘properties’: { ‘name’: {‘type’:’string’}, ‘hobbies’: {‘type’: ‘array’} }, ‘additionalProperties’: false }”); IsValid(JObject.Parse( @”{ ‘name’: … Read more

JSON.stringify, change the case of the key

You can use a JSON replacer to switch keys before writing. JSON.stringify(myVal, function (key, value) { if (value && typeof value === ‘object’) { var replacement = {}; for (var k in value) { if (Object.hasOwnProperty.call(value, k)) { replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k]; } } return replacement; } return value; }); For the … Read more

How to create a custom deserializer in Jackson for a generic type?

You can implement a custom JsonDeserializer for your generic type which also implements ContextualDeserializer. For example, suppose we have the following simple wrapper type that contains a generic value: public static class Wrapper<T> { public T value; } We now want to deserialize JSON that looks like this: { “name”: “Alice”, “age”: 37 } into … Read more

Return results of a sql query as JSON in oracle 12c

Oracle 12c version 12.1.0.2 (the latest version as of 11.11.2014) adds JSON support: https://docs.oracle.com/database/121/NEWFT/chapter12102.htm#BGBGADCC It’s been available since October 17th. https://blogs.oracle.com/db/entry/oracle_database_12c_release_1 If you are unable to patch/work with that version there is an excellent package written by Lewis Cunningham and Jonas Krogsboell: PL/JSON * http://pljson.sourceforge.net/ It’s an excellent package (I have used it in numerous … Read more

JSON GSON.fromJson Java Objects

The JSON is invalid. A collection is not to be represented by {}. It stands for an object. A collection/array is to be represented by [] with commaseparated objects. Here’s how the JSON should look like: { users:[{ name: “name1”, email: “email1”, friends:[{ name: “name2”, email: “email2”, friends:[{ name: “name3”, email: “email3” }, { name: … Read more

PHP’s json_encode does not escape all JSON control characters

function escapeJsonString($value) { # list from www.json.org: (\b backspace, \f formfeed) $escapers = array(“\\”, “https://stackoverflow.com/”, “\””, “\n”, “\r”, “\t”, “\x08”, “\x0c”); $replacements = array(“\\\\”, “\\/”, “\\\””, “\\n”, “\\r”, “\\t”, “\\f”, “\\b”); $result = str_replace($escapers, $replacements, $value); return $result; } I’m using the above function which escapes a backslash (must be first in the arrays) and … Read more

Jackson 2.0 with Spring 3.1

Keith Donald (of spring source) tweeted the following a while back. Spring MVC support for Jackson 2; also works with Jackson’s native “pretty print” feature https://gist.github.com/2423129 I haven’t tried the MappingJackson2HttpMessageConverter found in the gist above but it would surprise me if it did not work.

Are all JSON objects also valid JavaScript objects?

Update 2019: the answer is now YES as of this proposal and JavaScript versions following ECMAScript 2019 (including) will be proper supersets. TL;DR The answer is “no”. There are cases when JSON object won’t be valid for JavaScript. JSON is NOT a JavaScript subset. “Little” difference JSON That is: due to JSON specification, you can … Read more