49

How do I declare a property or variable name as fg+ in C#?

public string fg+ { get; set; }

I am getting fg+ as a field in a response from JSON as "fg+": "103308076644479658279".

I am deserializing it using a class. How do I read it from JSON?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
  • 10
    @ChrisCirefice: Someone who expects the values to be accessed with `obj["key"]` syntax rather than `obj.key`. – dan04 Mar 15 '16 at 16:29
  • 6
    @dan04 Sure, but if you're using a JSON library like Google GSON to map JSON to Java objects or Json.NET for C#, you're generally going to have a bad time with a lot of weird exceptions for no *apparent* reason. Yes, JSON keys can be strings, but that shouldn't stop the person creating the JSON to be consumed from considering who has to *parse* it. It's best to avoid special symbols for things that act as *identifiers* in my opinion. I would never create such bizarre JSON unless I was deliberately trying to piss people off. – Chris Cirefice Mar 15 '16 at 17:14
  • Here is the spec on what is allowed in a var name: https://msdn.microsoft.com/en-us/library/aa664670.aspx – Max von Hippel Mar 15 '16 at 20:08
  • @ChrisCirefice, what about reserved identifiers? Should we abstain from using keys such as "for", "break", "from", etc.? – sleblanc Mar 15 '16 at 21:56
  • 3
    @sleblanc In my experience using JSON parsing libraries, it's completely dependent on your target consumers. For example, in using the Gson Java library for parsing JSON into Java objects, having a JSON key named `end` wouldn't be a problem because it's not a Java keyword. However, you can't name a Java variable `for`, because it's a keyword for a loop. Granted, most libraries like Gson allow for overriding the keys, so you could have a Java variable called `forDuration` corresponding to the JSON key `for`, but why would you want to potentially cause problems if they are easily avoided? – Chris Cirefice Mar 15 '16 at 22:03
  • @sleblanc And personally, I prefer to give my variables *meaningful* names. What does `for` mean? A duration? For a person? What does `from` mean? You can apply the same logic. I would rather use something like `start_date` and `end_date` for durations and such, and `sender` and `receiver`. Not encoding semantic information in JSON key names is as bad as having variables in any language named `s` when you actually mean `string`, `v` when you actually mean `version`, `c` when you actually mean `count`, etc. – Chris Cirefice Mar 15 '16 at 22:06
  • 4
    That just sounds like poor design. I shouldn't have to take into account the rules of the language a parser is written in when I use an encoding scheme. Direct mapping of JSON fields to variables sounds like a really bad decision. – David Schwartz Mar 16 '16 at 04:36
  • Go buy a baseball bat and show it to whoever wrote that JSON property. Wink once (not twice), and walk away. – Gil Sand Mar 17 '16 at 09:07
  • @Zil, sure :). Thanks for suggestion. – Musakkhir Sayyed Mar 17 '16 at 09:24
  • 1
    Possible duplicate of [How can I parse a JSON string that would cause illegal C# identifiers?](http://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers) – Peter O. Mar 18 '16 at 06:10

1 Answers1

118

Such a variable name isn't legal in C#.

If you're using Json.NET to deserialize your data, you can use the JsonProperty attribute to give it a custom name:

[JsonProperty("fg+")]
public string Fg { get; set; }
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 5
    Your answer is correct..but I think `key value pair` dictionary will support it in c#. – Pankaj Parkar Mar 15 '16 at 10:31
  • 50
    @PankajParkar Well, his question wasn't *"how can I generically deserialize my JSON into a `Dictionary`"*, he wanted to know how to deserialize it *directly to a property*. Of course, the former is possible. – Yuval Itzchakov Mar 15 '16 at 10:32