What is the difference between Serialization and Marshaling?

前端 未结 12 1499
忘了有多久
忘了有多久 2020-12-02 03:30

I know that in terms of several distributed techniques (such as RPC), the term "Marshaling" is used but don\'t understand how it differs from Serialization. Aren\'

相关标签:
12条回答
  • 2020-12-02 03:55

    Marshalling is usually between relatively closely associated processes; serialization does not necessarily have that expectation. So when marshalling data between processes, for example, you may wish to merely send a REFERENCE to potentially expensive data to recover, whereas with serialization, you would wish to save it all, to properly recreate the object(s) when deserialized.

    0 讨论(0)
  • 2020-12-02 04:03

    Marshaling and serialization are loosely synonymous in the context of remote procedure call, but semantically different as a matter of intent.

    In particular, marshaling is about getting parameters from here to there, while serialization is about copying structured data to or from a primitive form such as a byte stream. In this sense, serialization is one means to perform marshaling, usually implementing pass-by-value semantics.

    It is also possible for an object to be marshaled by reference, in which case the data "on the wire" is simply location information for the original object. However, such an object may still be amenable to value serialization.

    As @Bill mentions, there may be additional metadata such as code base location or even object implementation code.

    0 讨论(0)
  • 2020-12-02 04:06

    Marshaling uses Serialization process actually but the major difference is that it in Serialization only data members and object itself get serialized not signatures but in Marshalling Object + code base(its implementation) will also get transformed into bytes.

    Marshalling is the process to convert java object to xml objects using JAXB so that it can be used in web services.

    0 讨论(0)
  • 2020-12-02 04:07

    Here's more specific examples of both:

    Serialization Example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    typedef struct {
        char value[11];
    } SerializedInt32;
    
    SerializedInt32 SerializeInt32(int32_t x) 
    {
        SerializedInt32 result;
    
        itoa(x, result.value, 10);
    
        return result;
    }
    
    int32_t DeserializeInt32(SerializedInt32 x) 
    {
        int32_t result;
    
        result = atoi(x.value);
    
        return result;
    }
    
    int main(int argc, char **argv)
    {    
        int x;   
        SerializedInt32 data;
        int32_t result;
    
        x = -268435455;
    
        data = SerializeInt32(x);
        result = DeserializeInt32(data);
    
        printf("x = %s.\n", data.value);
    
        return result;
    }
    

    In serialization, data is flattened in a way that can be stored and unflattened later.

    Marshalling Demo:

    (MarshalDemoLib.cpp)

    #include <iostream>
    #include <string>
    
    extern "C"
    __declspec(dllexport)
    void *StdCoutStdString(void *s)
    {
        std::string *str = (std::string *)s;
        std::cout << *str;
    }
    
    extern "C"
    __declspec(dllexport)
    void *MarshalCStringToStdString(char *s)
    {
        std::string *str(new std::string(s));
    
        std::cout << "string was successfully constructed.\n";
    
        return str;
    }
    
    extern "C"
    __declspec(dllexport)
    void DestroyStdString(void *s)
    {
        std::string *str((std::string *)s);
        delete str;
    
        std::cout << "string was successfully destroyed.\n";
    }
    

    (MarshalDemo.c)

    #include <Windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    int main(int argc, char **argv)
    {
        void *myStdString;
    
        LoadLibrary("MarshalDemoLib");
    
        myStdString = ((void *(*)(char *))GetProcAddress (
            GetModuleHandleA("MarshalDemoLib"),
            "MarshalCStringToStdString"
        ))("Hello, World!\n");
    
        ((void (*)(void *))GetProcAddress (
            GetModuleHandleA("MarshalDemoLib"),
            "StdCoutStdString"
        ))(myStdString);
    
        ((void (*)(void *))GetProcAddress (
            GetModuleHandleA("MarshalDemoLib"),
            "DestroyStdString"
        ))(myStdString);    
    }
    

    In marshaling, data does not necessarily need to be flattened, but it needs to be transformed to another alternative representation. all casting is marshaling, but not all marshaling is casting.

    Marshaling doesn't require dynamic allocation to be involved, it can also just be transformation between structs. For example, you might have a pair, but the function expects the pair's first and second elements to be other way around; you casting/memcpy one pair to another won't do the job because fst and snd will get flipped.

    #include <stdio.h>
    
    typedef struct {
        int fst;
        int snd;
    } pair1;
    
    typedef struct {
        int snd;
        int fst;
    } pair2;
    
    void pair2_dump(pair2 p)
    {
        printf("%d %d\n", p.fst, p.snd);
    }
    
    pair2 marshal_pair1_to_pair2(pair1 p)
    {
        pair2 result;
        result.fst = p.fst;
        result.snd = p.snd;
        return result;
    }
    
    pair1 given = {3, 7};
    
    int main(int argc, char **argv)
    {    
        pair2_dump(marshal_pair1_to_pair2(given));
    
        return 0;
    }
    

    The concept of marshaling becomes especially important when you start dealing with tagged unions of many types. For example, you might find it difficult to get a JavaScript engine to print a "c string" for you, but you can ask it to print a wrapped c string for you. Or if you want to print a string from JavaScript runtime in a Lua or Python runtime. They are all strings, but often won't get along without marshaling.

    An annoyance I had recently was that JScript arrays marshal to C# as "__ComObject", and has no documented way to play with this object. I can find the address of where it is, but I really don't know anything else about it, so the only way to really figure it out is to poke at it in any way possible and hopefully find useful information about it. So it becomes easier to create a new object with a friendlier interface like Scripting.Dictionary, copy the data from the JScript array object into it, and pass that object to C# instead of JScript's default array.

    test.js:

    var x = new ActiveXObject("Dmitry.YetAnotherTestObject.YetAnotherTestObject");
    
    x.send([1, 2, 3, 4]);
    

    YetAnotherTestObject.cs

    using System;
    using System.Runtime.InteropServices;
    
    namespace Dmitry.YetAnotherTestObject
    {
        [Guid("C612BD9B-74E0-4176-AAB8-C53EB24C2B29"), ComVisible(true)]
        public class YetAnotherTestObject
        {
            public void send(object x)
            {
                System.Console.WriteLine(x.GetType().Name);
            }
        }
    }
    

    above prints "__ComObject", which is somewhat of a black box from the point of view of C#.

    Another interesting concept is that you might have the understanding how to write code, and a computer that knows how to execute instructions, so as a programmer, you are effectively marshaling the concept of what you want the computer to do from your brain to the program image. If we had good enough marshallers, we could just think of what we want to do/change, and the program would change that way without typing on the keyboard. So, if you could have a way to store all the physical changes in your brain for the few seconds where you really want to write a semicolon, you could marshal that data into a signal to print a semicolon, but that's an extreme.

    0 讨论(0)
  • 2020-12-02 04:08

    I think that the main difference is that Marshalling supposedly also involves the codebase. In other words, you would not be able to marshal and unmarshal an object into a state-equivalent instance of a different class. .

    Serialization just means that you can store the object and reobtain an equivalent state, even if it is an instance of another class.

    That being said, they are typically synonyms.

    0 讨论(0)
  • 2020-12-02 04:08

    Basics First

    Byte Stream - Stream is sequence of data. Input stream - reads data from source. Output stream - writes data to desitnation. Java Byte Streams are used to perform input/output byte by byte(8bits at a time). A byte stream is suitable for processing raw data like binary files. Java Character Streams are used to perform input/output 2 bytes at a time, because Characters are stored using Unicode conventions in Java with 2 bytes for each character. Character stream is useful when we process(read/write) text files.

    RMI(Remote Method Invocation) - an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.


    Both Serialization and Marshalling are loosely used as synonyms. Here are few differences.

    Serialization - Data members of an object is written to binary form or Byte Stream(and then can be written in file/memory/database etc). No information about data-types can be retained once object data members are written to binary form.

    Marshalling - Object is serialized(to byte stream in binary format) with data-type + Codebase attached and then passed Remote Object(RMI). Marshalling will transform the data-type into a predetermined naming convention so that it can be reconstructed with respect to the initial data-type.

    So Serialization is part of Marshalling.

    CodeBase is information that tells the receiver of Object where the implementation of this object can be found. Any program that thinks it might ever pass an object to another program that may not have seen it before must set the codebase, so that the receiver can know where to download the code from, if it doesn't have the code available locally. The receiver will, upon deserializing the object, fetch the codebase from it and load the code from that location. (Copied from @Nasir answer)

    Serialization is almost like a stupid memory-dump of the memory used by the object(s), while Marshalling stores information about custom data-types.

    In a way, Serialization performs marshalling with implematation of pass-by-value because no information of data-type is passed, just the primitive form is passed to byte stream.

    Serialization may have some issues related to big-endian, small-endian if the stream is going from one OS to another if the different OS have different means of representing the same data. On the other hand, marshalling is perfectly fine to migrate between OS because the result is a higher-level representation.

    0 讨论(0)
提交回复
热议问题