JProperty with raw JSON

Sep 26, 2008 at 1:46 PM
Edited Sep 26, 2008 at 1:47 PM
Hi

Is there a way of getting a JProperty to take raw JSON as its value or another way of achieving the same thing?  I have JSON fragments stored in a cache which at some earlier stage would have had ToString called on a root JObject.  The only way I've found of achieving this so far is to call JObject.Parse(raw) for the value part though this seems somewhat wasteful as it was valid JSON just created at another time.  My intention is to create anew JSON object to return to the caller where parts of it have been cached separately.

Kind regards

Duncan

Coordinator
Sep 27, 2008 at 11:50 PM
I have made some changes and renamed Identifier to JsonRaw. If you make it the property type on the class you want to serialize I think it should do what you are looking for.
Sep 29, 2008 at 11:53 AM
Hi

Thanks, I must be missing something as it doesn't quite work the way I expect.  I've tried two different approaches and I get the same error about invalid state. 
The first approach I tried was passing an array of JProperty("d" + i, JValue.CreateRaw(rawJson)) to a JObject.  The 2nd approach is to use the lower level writer:

                jsonWriter.WriteStartObject();

                while (loopCondition)
                {
                    jsonWriter.WritePropertyName("d" + i);
                    jsonWriter.WriteRaw(rawJSon);

                    i++;
                }
                jsonWriter.WriteEndObject();               

The value of the property will essentially be some pre-existing object cached earlier.  There is no class that will contain these properties.

Kind regards

Duncan