Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:41:00+00:00 2026-06-02T23:41:00+00:00

I am having problem to find some information about how to get a custom

  • 0

I am having problem to find some information about how to get a custom converter invoked for a given property when serializing a C# object to JSON (and vice-versa).

JSON looks like that:

{"ws_status": "success", "result": 32}
or
{"ws_status": "failure", "error_code": 32123}

I have my C# object:

[DataContract]
class WebServiceResult
{

    [DataMember(Name = "ws_status", IsRequired = true)]
    public Boolean Success { get; private set; }

    [DataMember(Name = "error_code")]
    public Int32 ErrorCode { get; private set; }

    [DataMember(Name = "result")]
    public Int32 Result { get; private set; }
}

What I am missing is how to get the JSON values “success” and “failure” converted to a boolean telling me if the WS was successful.

I have implemented a JavaScriptConverter but I don’t know how to bind it to my C# object’s property.

class JsonStatusConverter : JavaScriptConverter
{
    public override IEnumerable<Type> SupportedTypes
    {
        get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(Boolean) })); }
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        // Here, do we get a WebServiceResult or directly the WebServiceResult.Success 
        // object?
        Boolean success = obj==null ? false : (Boolean) obj;

        Dictionary<string, object> result = new Dictionary<string, object>();
        result["ws_status"] = success ? "success" : "error";
        return result;
    }

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (dictionary == null)
            throw new ArgumentNullException("dictionary");

        if (type == typeof(String))
        {
            String status = dictionary["ws_status"] as String;

            if (status.Equals("success")) return true;
            else return false;
        }
        return null;
    }
}

Maybe I misunderstood the concept of JavaScriptConverter and it can only be implemented for the whole WebServiceResult object (which would be a pity because most of the properties are standard).

PS: I know I could simply get the status serialized directly to a string and have a helper method converting that to a boolean in the C# object, but I’d like to learn about converters as I will need them in some other objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:41:02+00:00Added an answer on June 2, 2026 at 11:41 pm

    JsonSerializer/JavaScriptSerializer and DataContractJsonSerializer are distinct entities. DataContractJsonSerializer does not support even the concept of JavaScriptConverters, and JavaScriptSerializer in turn does not honor the DataContract programming model. In addition, with DataContractJsonSerializer, there is no way to do what you want to do here, at all, because boolean is a fundamental primitive in its serialization model, and in its world, extensibility points for primitives are severely curtailed, if not non-existent.

    I think your best bet here (sadly) is to ditch this entire model altogether and use Json .NET

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m having some difficulties with two queries. I can get some of the information
I'm having a problem where I can't find images through Java. My friend and
I'm having a problem in ruby and I can't seem to find the solution
I'm having a problem and can't seem to find the solution.. int linearSearch(nodeptr list,char
So I searched and couldn't find anyone else having this problem: When I try
I'm having this problem, same as ever, but never try to find the right
I am having a strange problem ( well, at least i find it strange
I've already looked around but couldn't find the exact solution/problem I'm having right now.
I have exhausted my efforts to find any useful information about the IAuthorizeRemotingConnection interface
Hi, i have been having some problems using JSON data in flash builder lately

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.