6

I've looked at various questions but I am unsure of how to implement this.

I have a custom struct, which currently has no public properties on it. When it is returned via WebApi (not doing any fancy serialization, just returning the custom struct itself), it is returned as an object {}.

public struct CustomStruct
{
    private string myProperty;

    ...

    public override string ToString()
    {
      return this.myProperty;
    }

    ...

}

The custom struct itself is the type of a property on a parent class, which serializes to:

{ "MyProp1":"value1","MyProp2":"value2","MyCustomStruct":{} }

When I override ToString() on the custom struct I want to output one of the private properties. Can I achieve a similar behaviour when returning the object to JavaScript-land, as a JSON object?

E.g. my private property is a string, called "myProperty", set to "test". If I added a public property called "MyProperty", I'd get the following output:

{ "MyProp1":"value1","MyProp2":"value2","MyCustomStruct":{ "MyProperty":"test" } }

When what I really want is:

{ "MyProp1":"value1","MyProp2":"value2","MyCustomStruct":"test" }

Hope this makes sense.

Here are the related questions that haven't really helped me much. Would like to avoid using JSON.NET if possible but will go for that if it is the only way:

JSON.Net Struct Serialization Discrepancy

C# custom json serialization

JSON.NET with Custom Serializer to a custom object

JSON serialization of enum as string

JavaScriptSerializer.Deserialize - how to change field names

2
  • 1
    What are you currently using for serialization? i.e. what version of WebAPI? If it's the latest version you're already using JSON.Net if I'm not mistaken. Commented Aug 16, 2013 at 16:37
  • MVC 4, .NET 4.0... yes there is a reference to JSON.NET created by default, I could use it, at the moment I am simply returning a collection of my classes, not an ActionResult/ContentResult or anything like that. Commented Aug 19, 2013 at 8:26

2 Answers 2

5

I faced the same challenge. I solved it by writing a custom JsonConverter that uses the objects ToString method when converting:

public class ToStringConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanConvert(Type objectType)
    {
        return true;
    }
}

And then using it like this as JsonConverter attribute:

[JsonConverter(typeof(ToStringConverter))]
public AnotherObjectWithToString MyObject { get; set; }

I should note that this can only be used for serialization as deserialization would require the ToString result to converted back into a object and that will vary by type.

Sign up to request clarification or add additional context in comments.

Comments

1

What I have done for now, is add a second property on the parent CustomClass class...

public string MyCustomStructValue { get { return MyCustomStruct.ToString(); } }

then add the [IgnoreDataMember] attribute to the original property...

[IgnoreDataMember]
public CustomStruct MyCustomStruct { get; set; }

which works fine with the following action:

public IEnumerable<CustomClass> Get()
{
    return GetResults();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.