GithubHelp home page GithubHelp logo

Comments (6)

asbjornu avatar asbjornu commented on July 20, 2024

Why are you using typeof(object) and not typeof(Speicherstand)? Ok, saw your edit now. You need to identify the type and send it over the network, somehow. Most HTTP frameworks do this through a custom header (like RavenDB's Raven-Clr-Type) or through a custom Content-Type (like Content-Type: vnd.company-x/speicherstand+xml). If you don't tell Yax what type it's supposed to deserialize, how is it going to know?

from yaxlib.

weini37 avatar weini37 commented on July 20, 2024

Hello, thanks for the comment.
I cant use typeof(Speicherstand), because the api i wrote does not know what the object type is.
I wrote a method which basically receives a message containing a xml serialized with yaxlib. then the method deserializes the xml to a object. after that in the caller method i could simply cast it to the given object it needs to be.

The problem is here, that the serialization comes from over the network so basically i dont know which object it is. e.g. "Speicherstand" or "quit" or something like that.

from yaxlib.

Nadynho avatar Nadynho commented on July 20, 2024

Yes, but then how should the deserializer interpret the meta of object type? Object is not enough..., at least an interface would be nice....

On Friday, November 7, 2014 4:40 PM, Michael Weinberger [email protected] wrote:

Hello, thanks for the comment.
I cant use typeof(Speicherstand), because the api i wrote does not know what the object type is.
I wrote a method which basically receives a message containing a xml serialized with yaxlib. then the method deserializes the xml to a object. after that in the caller method i could simply cast it to the given object it needs to be.

Reply to this email directly or view it on GitHub.

from yaxlib.

weini37 avatar weini37 commented on July 20, 2024

Object is not enough.

Why does the serializer then writes in the first tag: yaxlib:realtype=\"WpfApplication2.Speicherstand\"?
Should not it detect it then afterwards again and create one via the properties?

from yaxlib.

304NotModified avatar 304NotModified commented on July 20, 2024

I think is a feature request.

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

Expressing this issue in code and conclude:
XML can only be deserialized to a specific class, when

  • the serializer instance is created for this class, OR
  • the serializer is created for typeof(object), but the XML contains the yaxlib:realtype attribute.
namespace YAXLibTests;
public class Issue31SampleClass
{
    public string Username { get; set; }
    public int Granny { get; set; }
    public int Factory { get; set; }
    public int Farm { get; set; }
    public int Mine { get; set; }
    public int Cursor { get; set; }
}

[TestFixture]
public class Issue_31
{
    [Test]
    public void DeSerialize()
    {
        var original = new Issue31SampleClass { Cursor = 1, Factory = 2, Farm = 3, Granny = 4, Mine = 5 };
        var s = new YAXSerializer(typeof(object));
        var xml = s.Serialize(original);
/*
<Object yaxlib:realtype="YAXLibTests.Issue31SampleClass" xmlns:yaxlib="http://www.sinairv.com/yaxlib/">
  <Username />
  <Granny>4</Granny>
  <Factory>2</Factory>
  <Farm>3</Farm>
  <Mine>5</Mine>
  <Cursor>1</Cursor>
</Object>
 */
        var deserialized = (Issue31SampleClass) s.Deserialize(xml);
        deserialized.Should().BeEquivalentTo(original);

        var xmlWithOutRealType =
            xml.Replace(
                "<Object yaxlib:realtype=\"YAXLibTests.Issue31SampleClass\" xmlns:yaxlib=\"http://www.sinairv.com/yaxlib/\">",
                "<Object>");

        // Create a new serializer instance!
        s = new YAXSerializer(typeof(object));
        Assert.That(() =>
        {
            // The serializer cannot guess which type to create
            _ = (Issue31SampleClass) s.Deserialize(xmlWithOutRealType);
        }, Throws.TypeOf(typeof(InvalidCastException)));
    }
}

from yaxlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.