JSON serialization without escaped quotes

2,461 views
Skip to first unread message

Ryan

unread,
Aug 6, 2011, 10:48:01 AM8/6/11
to mongodb-csharp
I have the below code snippet that I was hoping would return a bit
cleaner JSON. It would be nice to drop the leading and trailing quotes
as well as the escapes before every quote. I have been using the JSON
strict setting, but I feel like I am missing something. If I try to
return a Stream instead of a string I get a serialization error. Any
advice??

public string GetBsRobust(string bID, double latitude, double
longitude, double radius)
{
var nearQuery = Query.Near("bLocation", latitude, longitude,
radius);

ObjectId queryId = new ObjectId(bID);
var query = new QueryDocument {
{ "_id", queryId } };

BsonDocument retBs = _bs.FindOne(query);

MongoCursor csrBs = _beeps.Find(nearQuery).SetLimit(10);

var settings = new JsonWriterSettings
{
OutputMode =
JsonOutputMode.Strict
};
List<BsonDocument> retList = new List<BsonDocument>();

foreach (B bs in csrBs)
{
retList.Add(bs.ToBsonDocument());
}

return retList.ToJson(settings);
}

and here is a bit of the JSON returned

"[{ \"_id\" : { \"$oid\" : \"4e33a29d7ac94b160850b751\" }, \"sfID\" :
\"\", \"acctDisplayName\" : \"My House\", \"account\" :
\"001E000011101plcPIAQ\", \"address1\" : \"1234 Main St.\",
\"address2\" : \"\"......

Thanks!!!

Robert Stam

unread,
Aug 6, 2011, 11:26:13 AM8/6/11
to mongodb-csharp
I think the extra quotes and escaping backslashes you are seeing are
because you are looking at the string in the Visual Studio debugger.
The actual JSON produced doesn't have them.

You can write JSON to a file with Strict JsonOutputMode like this:

var document = new BsonDocument {
{ "_id", ObjectId.GenerateNewId() },
{ "data", "abc" }
};
using (var textWriter = new StreamWriter("test.json")) {
var jsonWriterSettings = new JsonWriterSettings { OutputMode =
JsonOutputMode.Strict };
using (var bsonWriter = BsonWriter.Create(textWriter,
jsonWriterSettings)) {
BsonSerializer.Serialize(bsonWriter, document);
}
}

You can do something similar to write to a Stream, but remember that
the JsonWriter writes to a TextWriter, not a Stream, so you would have
to wrap a TextWriter around your stream.

Ryan

unread,
Aug 8, 2011, 1:47:34 AM8/8/11
to mongodb-csharp
Hmmm,
I am trying to wrap the response in a restful web service so I have to
return something. Maybe I will try the second part of your suggestion
and instead of wrapping a json file in a streamwriter I'll just wrap a
list of bson documents in a stream? I don't know, I'll keep trying,
but when I call the above code from a URL I get quoted JSON as the
file returned. I'm sure it's because I am returning a string, but I
don't know how to call a stream without getting an error. I'll explore
the error message some more as well.
Thanks for the insight though,
Ryan

Ryan

unread,
Aug 8, 2011, 2:01:31 PM8/8/11
to mongodb-csharp
For completeness, here is the bit of code I was missing.
Because I am sending everything through a WCF service it wants to deal
in strings. You have to explicitely return a stream and simply do
this:

WebOperationContext.Current.OutgoingResponse.ContentType =
"application/json; charset=utf-8";
return new
MemoryStream(Encoding.UTF8.GetBytes(retList.ToJson(settings)));

so the complete code looks like:

public Stream GetBsRobust(string bID, double latitude, double
longitude, double radius)
{
var nearQuery = Query.Near("bLocation", latitude, longitude,
radius);

ObjectId queryId = new ObjectId(bID);
var query = new QueryDocument {
{ "_id", queryId } };

BsonDocument retBs = _bs.FindOne(query);

MongoCursor csrBs = _beeps.Find(nearQuery).SetLimit(10);

var settings = new JsonWriterSettings
{
OutputMode =
JsonOutputMode.Strict
};
List<BsonDocument> retList = new List<BsonDocument>();

foreach (B bs in csrBs)
{
retList.Add(bs.ToBsonDocument());
}

WebOperationContext.Current.OutgoingResponse.ContentType =
"application/json; charset=utf-8";
return new
MemoryStream(Encoding.UTF8.GetBytes(retList.ToJson(settings)));
}

Hope this helps someone!!!
Reply all
Reply to author
Forward
0 new messages