0

I have a problem where i need to output an object from a wcf restful service with a $ in it. I've tried outputting a dictionary but when that serializes to JSON the $gets encoded. The service i am communicating with requires the object to look like this [{$name : value}]

Is it possible to change the Name in the data member attribute on a parameter at runtime?

[DataMember(Name = "RenameMe")]
public string Values { get; set; }
public Variable(string name, string values)
{           
      this.Values = values; 
      [set the name attribute to : "$name"]


}
Avennite
  • 43
  • 7
  • 1
    Can you use newtonsoft? https://stackoverflow.com/questions/4638585/how-do-c-sharp-classes-deal-with-dollar-signs-in-json – Phil-R Jul 24 '18 at 18:37
  • That unfortunately does not help me since the name of the property will be different between variables. – Avennite Jul 24 '18 at 18:43
  • 1
    This isn't easy with WCF. You want to return a dictionary while setting [`DataContractJsonSerializerSettings.UseSimpleDictionaryFormat`](https://stackoverflow.com/questions/33773239/) to `true` - but this setting is never exposed, so you'll need to replace the entire serializer. See https://stackoverflow.com/questions/6792785 or https://stackoverflow.com/questions/11003016. Possibly https://stackoverflow.com/questions/33554997 will be required also. But if you just need to **return** a dictionary, https://stackoverflow.com/questions/35490329 may be the easier solution. – dbc Jul 24 '18 at 20:23
  • Thank you. Both of you have lead me down the route that fixed the issue. I stopped using the data contract serializer and went down the route of using newton soft. Then I output a Message type as the return type. – Avennite Jul 25 '18 at 15:02

0 Answers0