Mixing quoted and unquoted property names

Sep 21, 2009 at 4:32 PM

I am trying to create a JSON string out of a Dictionary class, where the keys are quoted, but other properties are not. Is this possible with a custom JsonConverter? Is there any other way to do this?

As an example, I have the following class:

public class ContentItemStatus {
			
	private DateTime lastWriteTime;

	[JsonConverter(typeof(IsoDateTimeConverter))]
	public DateTime LastWriteTime {
		get { return lastWriteTime; }
		set { lastWriteTime = value; }
	}
		
	public ContentItemStatus(DateTime value) {
		this.lastWriteTime = value;
	}
}

I am trying to serialize this class in a Dictionary<string, ContentItemStatus>, where the string represents a filename.

With QuoteName set to false, I get:

 

{
  C:\foo\bar.txt: {
    LastWriteTime: "2009-09-21T10:12:39.578536-05:00"
  },
  C:\bar\foo.txt: {
    LastWriteTime: "2009-09-21T10:12:39.578536-05:00"
  },
  D:\: {
    LastWriteTime: "2009-09-21T10:12:39.578536-05:00"
  }
}

 

Which, of course, can't be deserialized back in. However, with QuoteName set to true, I get:

{
  "C:\foo\bar.txt": {
    "LastWriteTime": "2009-09-21T10:19:31.8413408-05:00"
  },
  "C:\bar\foo.txt": {
    "LastWriteTime": "2009-09-21T10:19:31.8413408-05:00"
  },
  "D:\": {
    "LastWriteTime": "2009-09-21T10:19:31.8413408-05:00"
  }
}

Which is okay, but, what I really want is this:

{
  "C:\foo\bar.txt": {
    LastWriteTime: "2009-09-21T10:19:31.8413408-05:00"
  },
  "C:\bar\foo.txt": {
    LastWriteTime: "2009-09-21T10:19:31.8413408-05:00"
  },
  "D:\": {
    LastWriteTime: "2009-09-21T10:19:31.8413408-05:00"
  }
}

Sep 21, 2009 at 6:49 PM

Oops, nevermind... I was befuddled by a confusion between valid JSON and Javascript object literals. I'm going to have to rethink my choices here.

Although, if anybody has a solution to the original problem, I'd still appreciate it.