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 8374669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:58:20+00:00 2026-06-09T14:58:20+00:00

How do we preserve the object type in json string when sending data to

  • 0

How do we preserve the object type in json string when sending data to asmx web service in .net 2.0?

for example:

class A{
 string name;
}

class B : A {
 string address;
}

and the web method:

[WebMethod]
public void PushJson(A obj){
  B b = (B) obj;
}

Now in above example scenario, lets say, i send {"obj":{"name":"waqas","address":"sweden"}} then how can i force my json string to act as type of class B, so that it can be accepted by web method as an object of class A and further parsed back into object of class B? in short, how to preserve polymorphism in json?

I’ve noticed that the compiler throws me System.InvalidCastException when ever i try to execute such kind of pattern

P.S. I’ve noticed that .net adds __type for complex objects while serializing into json. Is it possible that we can include this key to help .net to automatically parse json string with correct class type?

any help/suggestion would be helpful.


Update:

If we observe carefully the wsdl of an asmx web-service, so the objects whose classes inherits parent classes are containing something like <s:extension base="tns:ParentClassName">. I think this extension part is the thing which I may need to convert into Json. Any idea regarding this?

  • 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-09T14:58:22+00:00Added an answer on June 9, 2026 at 2:58 pm

    You mention GSON in your title, but I’m not sure where it is playing into this picture. So, I might be off on the wrong tangent. However, if you are just asking about getting .NET to deserialize your JSON, yes, you can use the __type parameter. It must come first.

    {"obj":{"__type":"B","name":"waqas","address":"sweden"}}
    

    I was able to get this to work in a test project, but like I said, no GSON involved.

    EDIT:
    Actually, you might also want to see this other answer https://stackoverflow.com/a/10805715/1539015 that talks about how to get GSON to include that __type parameter.

    EDIT2:
    I made a new .NET web site. Added a class file with your A and B classes (modified to make them public):

    public class A
    {
        public string name;
    }
    
    public class B : A
    {
        public string address;
    }
    

    I then added a web service to have the WebMethod you had in the question. I also included a GetJson method. Here’s the code behind:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {
    
        public WebService () {
        }
    
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        [WebMethod]
        public B GetJson()
        {
            return new B() { address = "addr", name = "nm" };
        }
    
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public string PushJson(A obj)
        {
            B b = (B)obj;
            return b.address;
        }
    }
    

    I then edited the default page to call the web method using jQuery:

    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <p>
            <div id="GetResult">Click here for the result of getting json.</div>    
            <div id="PushResult">Click here for the result of pushing json.</div>    
        </p>
        <script type="text/javascript">
            $(document).ready(function () {
                // Add the page method call as an onclick handler for the div.
                $("#GetResult").click(function () {
                    $.ajax({
                        type: "GET",
                        url: "WebService.asmx/GetJson",
                        contentType: "application/json; charset=utf-8",
                        success: function (msg) {
                            // Replace the div's content with the page method's return.
                            $("#GetResult").text(msg.d.name);
                        }
                    });
                });
                $("#PushResult").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "WebService.asmx/PushJson",
                        data: '{"obj":{"__type":"B","name":"waqas","address":"sweden"}}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                            // Replace the div's content with the page method's return.
                            $("#PushResult").text(msg.d);
                        }
                    });
                });
            });  
        </script>
    </asp:Content>
    

    If you place a breakpoint in the webservice method of PushJson, you can see that the object that was created was of type B, and it runs also showing it can be cast to type B and used.

    There’s no GSON here, but I believe the other post I linked should show how to get GSON to generate the __type parameter.

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

Sidebar

Related Questions

Is there a way to create an xts object from a data.frame and preserve
Assume that we have an object k of type class A . We defined
I want to preserve a property between postbacks in an ASP.Net application. Currently doing
Is there any way to preserve spaces in a data.frame's column names when calling
How do I have JAXB preserve nulls when receiving a JSON sting that contains
Consider: Public MustInherit Class Column Public ReadOnly Property ReturnSomethingUseful() As Object Get 'return something
I'm using C# with .NET 3.5. I am using the System.Configuration.ApplicationSettingsBase class to load
I was reading Fowler's Refactoring Book and saw Preserve Whole Object . A different,
I have already object of CCompositePrimitive class in Main.cpp int main() { ... CCompositePrimitive
I need to remotely load a .NET DLL that contains an ActiveX object (non-visual)

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.