Enrollment 28.2.0
Loading...
Searching...
No Matches
Serialization Directory Reference
Directory dependency graph for Serialization:

Files

 
Base64Strategy.h
 
JsonWriter.h
 
Writer.h

Detailed Description

The serialization is used to transform internal classes into user defined output.

The serialization is used to transform internal classes into user defined output.

The library serialize itself via Writer, Serializable and BytesWriterStrategy interfaces.

Interfaces Writer,BytesWriterStrategy are defined for client extension. When client needs own serialization then client implements Writer interface and provide it as Writer for library classes. When client needs to modify only bytes serialization, client implements BytesWriterStrategy interface and sets it into Writer. The library itself provides JSON implementation in class JsonWriter. Each class in library that can be serialized must inherit Serializable interface.

Use built in JSON
  • c++
    static void UseJson()
    {
    auto fp = CreatePrint();
    JsonWriter writer;
    writer.WriteRoot(*fp);
    // print serialized output
    std::cout << " " << writer.Output() << std::endl;
    }
  • java
    public void useJson() {
    Print fp = createPrint();
    JsonWriter writer = new JsonWriter();
    writer.WriteRoot(fp);
    // print serialized output
    System.out.println(" " + writer.Output());
    }
  • csharp
    public void useJson()
    {
    Print fp = createPrint();
    JsonWriter writer = new JsonWriter();
    writer.WriteRoot(fp);
    // print serialized output
    System.Console.WriteLine(" " + writer.Output());
    }
Use built in JSON with Base64 encoded bytes
Use built in JSON with Base64 encoded bytes
  • c++
    void UseJsonWithBase64()
    {
    auto fp = CreatePrint();
    JsonWriter writer;
    Base64Strategy base64;
    writer.SetBytesStrategy(base64);
    writer.WriteRoot(*fp);
    // print serialized output
    std::cout << " " << writer.Output() << std::endl;
    }
  • java
    public void useJsonWithBase64() {
    Print fp = createPrint();
    JsonWriter writer = new JsonWriter();
    BytesWriterStrategy base64 = new Base64Strategy();
    writer.SetBytesStrategy(base64);
    writer.WriteRoot(fp);
    // print serialized output
    System.out.println(" " + writer.Output());
    }
  • csharp
    public void useJsonWithBase64()
    {
    Print fp = createPrint();
    JsonWriter writer = new JsonWriter();
    BytesWriterStrategy base64 = new Base64Strategy();
    writer.SetBytesStrategy(base64);
    writer.WriteRoot(fp);
    // print serialized output
    System.Console.WriteLine(" " + writer.Output());
    }
Use client specific serialization
Use client specific serialization
  • c++
    class ClientWriter : public Writer
    {
    public:
    std::string text;
    void Write(const char* name, const Serializable& s) final
    {
    text += name;
    text += "(";
    s.WriteTo(*this);
    text += ")";
    }
    void WriteRoot(Serializable& s) final
    {
    s.WriteTo(*this);
    }
    void WriteNull(const char* name) final
    {
    text += name;
    text += "(";
    text += "null";
    text += ")";
    }
    void Write(const char* name, const char* value) final
    {
    text += name;
    text += "(";
    text += value;
    text += ")";
    }
    void Write(const char* name, double value) final
    {
    Write(name, std::to_string(value).c_str());
    }
    void Write(const char* name, int64_t value) final
    {
    Write(name, std::to_string(value).c_str());
    }
    void Write(const char* name, uint64_t value) final
    {
    Write(name, std::to_string(value).c_str());
    }
    void Write(const char* name, int value) final
    {
    Write(name, std::to_string(value).c_str());
    }
    void Write(const char* name, unsigned int value) final
    {
    Write(name, std::to_string(value).c_str());
    }
    void Write(const char* name, const std::shared_ptr<Image>& value) final
    {
    auto w = std::to_string(value->Width());
    auto h = std::to_string(value->Height());
    const std::string desc = "w:" + w + ",h:" + h;
    Write(name, desc.c_str());
    }
    private:
    void StartArray(const char* /*name*/) final
    {
    // for simplicity we ignore arrays in example
    }
    void EndArray() final
    {
    // for simplicity we ignore arrays in example
    }
    };
    static void UseClientSpecificWriter()
    {
    auto fp = CreatePrint();
    ClientWriter writer;
    writer.WriteRoot(*fp);
    // print serialized output
    std::cout << " " << writer.text << std::endl;
    }
  • java
    class ClientWriter extends com.innovatrics.enrollment.Writer {
    public String text;
    public ClientWriter() {
    text = "";
    }
    public void Write(String name, com.innovatrics.enrollment.Serializable s) {
    text += name + "(";
    s.WriteTo(this);
    text += ")";
    }
    public void WriteRoot(com.innovatrics.enrollment.Serializable s) {
    s.WriteTo(this);
    }
    public void Write(String name, String value) {
    text += name + "(" + value + ")";
    }
    public void Write(String name, int value) {
    text += name + "(" + value + ")";
    }
    public void Write(String name, long value) {
    text += name + "(" + value + ")";
    }
    public void Write(String name, Image value) {
    Write(name, "w:" + value.Width() + ",h:" + value.Height());
    }
    protected void StartArray(String name) {
    // for simplicity we ignore arrays in example
    }
    protected void EndArray() {
    // for simplicity we ignore arrays in example
    }
    }
    public void useClientSpecificWriter() {
    Print fp = createPrint();
    ClientWriter writer = new ClientWriter();
    writer.WriteRoot(fp);
    // print serialized output
    System.out.println(" " + writer.text);
    }
  • csharp
    class ClientWriter : Writer
    {
    public string text;
    public ClientWriter()
    {
    }
    public override void Write(string name, Serializable s)
    {
    text += name + "(";
    s.WriteTo(this);
    text += ")";
    }
    public override void WriteRoot(Serializable s)
    {
    s.WriteTo(this);
    }
    public override void Write(string name, string value)
    {
    text += name + "(" + value + ")";
    }
    public override void Write(string name, int value)
    {
    text += name + "(" + value + ")";
    }
    public override void Write(string name, uint value)
    {
    text += name + "(" + value + ")";
    }
    public override void Write(string name, long value)
    {
    text += name + "(" + value + ")";
    }
    public override void Write(string name, ulong value)
    {
    text += name + "(" + value + ")";
    }
    public override void Write(string name, Image value)
    {
    Write(name, "w:" + value.Width() + ",h:" + value.Height());
    }
    protected override void StartArray(string name)
    {
    // for simplicity we ignore arrays in example
    }
    protected override void EndArray()
    {
    // for simplicity we ignore arrays in example
    }
    }
    public void useClientSpecificWriter()
    {
    Print fp = createPrint();
    ClientWriter writer = new ClientWriter();
    writer.WriteRoot(fp);
    // print serialized output
    System.Console.WriteLine(" " + writer.text);
    }
Use client specific bytes strategy
Use client specific bytes strategy
  • c++
    class ClientStrategy : public BytesWriterStrategy
    {
    public:
    void WriteBytes(Writer& writer,
    const char* name,
    const std::vector<uint8_t>& value) final
    {
    auto size = std::to_string(value.size());
    auto v0 = std::to_string(value[0]);
    auto v1 = std::to_string(value[1]);
    const std::string text = "Reduced 2/" + size + " " + v0 + "," + v1 + ", ...";
    writer.Write(name, text.c_str());
    }
    };
    void UseClientSpecificByteStrategy()
    {
    auto fp = CreatePrint();
    JsonWriter writer;
    ClientStrategy bytesStrategy;
    writer.SetBytesStrategy(bytesStrategy);
    writer.WriteRoot(*fp);
    // print serialized output
    std::cout << " " << writer.Output() << std::endl;
    }
  • java
    class ClientStrategy extends BytesWriterStrategy {
    public void WriteBytes(
    com.innovatrics.enrollment.Writer writer, String name, Bytes value) {
    int s = value.size();
    int v0 = value.get(0);
    int v1 = value.get(1);
    writer.Write(name, "Reduced 2/" + s + " " + v0 + "," + v1 + ", ...");
    }
    }
    public void useClientSpecificByteStrategy() {
    Print fp = createPrint();
    JsonWriter writer = new JsonWriter();
    ClientStrategy bytesStrategy = new ClientStrategy();
    writer.SetBytesStrategy(bytesStrategy);
    writer.WriteRoot(fp);
    // print serialized output
    System.out.println(" " + writer.Output());
    }
  • csharp
    class ClientStrategy : BytesWriterStrategy
    {
    public override void WriteBytes(Writer writer, string name, Bytes value)
    {
    int s = value.Count;
    int v0 = value[0];
    int v1 = value[1];
    writer.Write(name, "Reduced 2/" + s + " " + v0 + "," + v1 + ", ...");
    }
    }
    public void useClientSpecificByteStrategy()
    {
    Print fp = createPrint();
    JsonWriter writer = new JsonWriter();
    ClientStrategy bytesStrategy = new ClientStrategy();
    writer.SetBytesStrategy(bytesStrategy);
    writer.WriteRoot(fp);
    // print serialized output
    System.Console.WriteLine(" " + writer.Output());
    }