직렬화와 마샬링의 차이점은 무엇입니까?
RPC와 같은 여러 분산 기술 측면에서 "마샬링"이라는 용어가 사용되지만 직렬화와 어떻게 다른지 이해하지 못합니다. 둘 다 객체를 일련의 비트로 변환하지 않습니까?
관련 :
마샬링 및 직렬화는 원격 프로 시저 호출의 맥락에서 느슨하게 동의하지만 의도적으로 의미 상 다릅니다.
특히, 마샬링은 여기에서 거기로 매개 변수를 가져 오는 것이지만 직렬화는 구조화 된 데이터를 바이트 스트림과 같은 기본 형식으로 복사하는 것입니다. 이런 의미에서 직렬화는 마샬링을 수행하는 수단 중 하나이며 일반적으로 값별 의미를 구현합니다.
객체가 참조에 의해 마샬링 될 수도 있으며,이 경우 "와이어상의"데이터는 단순히 원래 객체에 대한 위치 정보이다. 그러나 이러한 개체는 여전히 직렬화를 중요하게 생각할 수 있습니다.
@Bill이 언급했듯이 코드 기반 위치 또는 객체 구현 코드와 같은 추가 메타 데이터가있을 수 있습니다.
둘 다 공통적으로 한 가지 일을합니다-그것은 객체를 직렬화 하는 것입니다. 직렬화는 객체를 전송하거나 저장하는 데 사용됩니다. 그러나:
- 직렬화 : 개체를 직렬화하면 해당 개체 내의 멤버 데이터 만 바이트 스트림에 기록됩니다. 실제로 객체를 구현하는 코드가 아닙니다.
- 마샬링 : 마샬링이라는 용어는 Object를 원격 객체 (RMI)로 전달하는 것에 대해 이야기 할 때 사용됩니다 . Marshalling Object에서 직렬화 됨 (멤버 데이터가 직렬화 됨) + 코드베이스가 첨부됩니다.
직렬화는 마샬링의 일부입니다.
CodeBase 는이 객체의 구현을 찾을 수있는 Object의 수신자에게 알려주는 정보입니다. 이전에 보지 못한 다른 프로그램으로 객체를 전달할 수 있다고 생각하는 프로그램은 코드베이스를 설정해야합니다. 따라서 코드를 로컬로 사용할 수없는 경우 수신자가 코드를 다운로드 할 위치를 알 수 있습니다. 수신자는 객체를 직렬화 해제 할 때 객체에서 코드베이스를 가져와 해당 위치에서 코드를로드합니다.
로부터 마샬링 (컴퓨터 과학) Wikipedia 기사 :
"marshal"이라는 용어는 Python 표준 라이브러리 1 에서 "serialize"와 동의어로 간주 되지만 Java 관련 RFC 2713에서는 동의어가 아닙니다.
객체를 "마샬링"한다는 것은 마샬링 된 객체가 "비 마샬링"될 때 객체의 클래스 정의를 자동으로로드하여 원래 객체의 사본을 얻는 방식으로 상태 및 코드베이스를 기록하는 것을 의미합니다. 직렬화 가능하거나 원격 인 모든 오브젝트를 마샬링 할 수 있습니다. 마샬링은 코드베이스를 기록한다는 점을 제외하면 직렬화와 비슷합니다. 마샬링은 마샬링이 원격 객체를 특별히 처리한다는 점에서 직렬화와 다릅니다. (RFC 2713)
객체를 "직렬화"한다는 것은 바이트 스트림을 객체의 복사본으로 다시 변환 할 수있는 방식으로 상태를 바이트 스트림으로 변환하는 것을 의미합니다.
따라서 마샬링 은 객체 의 코드베이스 를 상태 외에도 바이트 스트림에 저장합니다 .
주요 차이점은 Marshalling이 코드베이스와 관련이 있다고 생각합니다. 즉, 객체를 다른 클래스의 상태와 동등한 인스턴스로 마샬링 및 마샬링 해제 할 수 없습니다. .
직렬화는 객체가 다른 클래스의 인스턴스 인 경우에도 객체를 저장하고 동등한 상태를 다시 얻을 수 있음을 의미합니다.
즉, 일반적으로 동의어입니다.
마샬링은 함수의 서명과 매개 변수를 단일 바이트 배열로 변환하는 것을 말합니다. 특히 RPC를 목적으로합니다.
직렬화는 종종 전체 객체 / 객체 트리를 바이트 배열로 변환하는 것을 말합니다. Marshaling은 객체 매개 변수를 직렬화하여 메시지에 추가하고 네트워크를 통해 전달합니다. * 직렬화는 디스크에 저장하는 데에도 사용할 수 있습니다. *
마샬링 은 다른 환경 / 시스템에서 데이터가 표현되는 방식을 컴파일러에 알리는 규칙입니다. 예를 들어;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
두 개의 다른 문자열 값이 다른 값 유형으로 표시되는 것을 볼 수 있습니다.
직렬화 는 표현이 아닌 객체 내용 만 변환하고 (동일하게 유지됨) 직렬화 규칙 (내보낼 항목 또는없는 항목)을 따릅니다. 예를 들어 개인 값은 직렬화되지 않으며 공개 값은 yes이며 개체 구조는 동일하게 유지됩니다.
다음은 두 가지에 대한보다 구체적인 예입니다.
직렬화 예 :
#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.
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.
My understanding of marshalling is different to the other answers.
Serialization:
To Produce or rehydrate a wire-format version of an object graph utilizing a convention.
Marshalling:
To Produce or rehydrate a wire-format version of an object graph by utilizing a mapping file, so that the results can be customized. The tool may start by adhering to a convention, but the important difference is the ability to customize results.
Contract First Development:
Marshalling is important within the context of contract first development.
- Its possible to make changes to an internal object graph, while keeping the external interface stable over time. This way all of the service subscribers won't have to be modified for every trivial change.
- Its possible to map the results across different languages. For example from the property name convention of one language ('property_name') to another ('propertyName').
Think of them as synonyms, both have a producer that sends stuff over to a consumer... In the end fields of instances are written into a byte stream and the other end foes the reverse ands up with the same instances.
NB - java RMI also contains support for transporting classes that are missing from the recipient...
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.
'Programming' 카테고리의 다른 글
언제 실제로 noexcept를 사용해야합니까? (0) | 2020.02.15 |
---|---|
맨 페이지에서 Unix 명령 이름 뒤에 괄호 안의 숫자는 무엇을 의미합니까? (0) | 2020.02.15 |
MongoDB에서 동등한 SQL 조인을 어떻게 수행합니까? (0) | 2020.02.15 |
re.search와 re.match의 차이점은 무엇입니까? (0) | 2020.02.15 |
JavaScript (jQuery 또는 Vanilla)로 확인란을 선택 / 선택 취소 하시겠습니까? (0) | 2020.02.14 |