Save a value which is invalid json
I am surprised by the following behaviour can you confirm that this is correct. Using the following code:
OperationFuture<Boolean> future = couchbaseClient.set("key", 0, "{\"x\": }"); System.out.println(future.get().booleanValue());
This causes an invalid json value to be added to the database, but the spymemcached client returns a true, meaning the set was OK. The resulting object for key "key" is:
{ "_id": "key", "_rev": "9-54d7a4c976bbaae69ce136f350855854", "$flags": 0, "$expiration": 0, "$att_reason": "invalid_json", "_attachments": { "value": { "content_type": "application/content-stream", "revpos": 1, "digest": "md5-vG4IxeQGAHgaQTMvMxAbSQ==", "length": 7, "stub": true } } }
If this is correct should a code my application to check for objects with a $att_reason, or how do you handle this. The reason this arose is we had some json that was not being escaped properly and spent sometime trying to understand what was happening..
Thanks, I'll look into what you suggest.
But at a higher level, don't you think that the 'set' method should return an error if the json is invalid ?
Two reason that come to mind are:
1. Allowing people to choose to use Couchbase as a key-value store or a document database
2. Maintaining compatibility with Membase.
I think the way you are using Couchbase it makes sense that an error should be returned. What might be a good idea is if we had a checkbox in the web ui that you could check to say something like "Validate JSON". If it was checked then Couchbase would return an error. If it is not then you would be able to store both JSON and none JSON values.
I have filed an issue here: http://www.couchbase.org/issues/browse/MB-4373
You can follow the discussion. If others like your idea then maybe we will get it added.
Looks like we will be implementing something like this, but there is currently no time frame for when it will be finished.
You can't get access to the $att_reason through a get operation because JSON parameters with $ in front of them are only used by the server. What I recommend doing is having a function in your client program that checks to make sure that your JSON is formatted correctly. The other thing you can do is to write a map function that finds all keys that contain invalid JSON. That function looks like this:
function (doc) {
emit(doc.$att_reason, null);
}
And it will return to you a list of documents that contain invalid JSON.
Edit: Removed an incorrect statement
Let me know if you have any other questions.