Enrollment 28.2.0
Loading...
Searching...
No Matches

Provides interface for serialization of all classes. More...

#include <Writer.h>

Collaboration diagram for Writer:

Public Member Functions

virtual void Write (const char *name, const Serializable &s)=0
 Serialize serializable.
virtual void WriteRoot (Serializable &s)=0
 Serialize serializable as root object.
virtual void Write (const char *name, const char *value)=0
 Serialize string.
virtual void Write (const char *name, int value)=0
 Serialize int.
virtual void Write (const char *name, unsigned int value)=0
 Serialize unsigned int.
virtual void Write (const char *name, double value)=0
 Serialize double.
virtual void Write (const char *name, int64_t value)=0
 Serialize int64_t integer.
virtual void Write (const char *name, uint64_t value)=0
 Serialize uint64_t integer.
virtual void Write (const char *name, const std::shared_ptr< Image > &value)=0
 Serialize Image.
virtual void WriteNull (const char *name)=0
 Serialize null object.
void WriteArray (const char *name, const std::vector< uint8_t > &value)
 Serialize bytes from vector.
void SetBytesStrategy (BytesWriterStrategy &s)
 Set bytes serialization strategy .
template<typename T>
void WriteArray (const char *name, const T &array)
 Serialize array of elements that are suitable for range-based for loop.
void Write (const char *name, const std::shared_ptr< const Serializable > &s)
 Serialize const serializable given as shared_ptr.
template<typename T>
void Write (const char *name, const std::optional< T > &s)
 Serialize serializable given as shared_ptr.

Detailed Description

Provides interface for serialization of all classes.

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());
    }

Member Function Documentation

◆ SetBytesStrategy()

void SetBytesStrategy ( BytesWriterStrategy & s)
inline

Set bytes serialization strategy .

Parameters
sstrategy to serialize bytes

◆ Write() [1/10]

virtual void Write ( const char * name,
const char * value )
pure virtual

Serialize string.

Parameters
namename of value (key)
valuestring value

Implemented in JsonWriter.

◆ Write() [2/10]

virtual void Write ( const char * name,
const Serializable & s )
pure virtual

Serialize serializable.

Parameters
namename of serializable
sclass that implements Serializable interface

Implemented in JsonWriter.

Referenced by Write(), Write(), ExtractedFace::WriteTo(), ExtractedIris::WriteTo(), ICSTemplate::WriteTo(), IrisTemplate::WriteTo(), and PrintDetectorConfig::WriteTo().

Here is the caller graph for this function:

◆ Write() [3/10]

template<typename T>
void Write ( const char * name,
const std::optional< T > & s )
inline

Serialize serializable given as shared_ptr.

Parameters
namename of serializable
sclass that implements Serializable interface

References Write(), and WriteNull().

Here is the call graph for this function:

◆ Write() [4/10]

void Write ( const char * name,
const std::shared_ptr< const Serializable > & s )
inline

Serialize const serializable given as shared_ptr.

Parameters
namename of serializable
sclass that implements Serializable interface

References Write(), and WriteNull().

Here is the call graph for this function:

◆ Write() [5/10]

virtual void Write ( const char * name,
const std::shared_ptr< Image > & value )
pure virtual

Serialize Image.

Once the image serialization needs to be modified e.g. all images are serialized in specific format, then strategy can be used similar as BytesWriterStrategy.

Parameters
namename of value (key)
valueImage

Implemented in JsonWriter.

◆ Write() [6/10]

virtual void Write ( const char * name,
double value )
pure virtual

Serialize double.

Parameters
namename of value (key)
valuedouble value

Implemented in JsonWriter.

◆ Write() [7/10]

virtual void Write ( const char * name,
int value )
pure virtual

Serialize int.

Parameters
namename of value (key)
valueinteger value

Implemented in JsonWriter.

◆ Write() [8/10]

virtual void Write ( const char * name,
int64_t value )
pure virtual

Serialize int64_t integer.

Parameters
namename of value (key)
valueunsigned integer value

Implemented in JsonWriter.

◆ Write() [9/10]

virtual void Write ( const char * name,
uint64_t value )
pure virtual

Serialize uint64_t integer.

Parameters
namename of value (key)
valueunsigned integer value

Implemented in JsonWriter.

◆ Write() [10/10]

virtual void Write ( const char * name,
unsigned int value )
pure virtual

Serialize unsigned int.

Parameters
namename of value (key)
valueunsigned integer value

Implemented in JsonWriter.

◆ WriteArray() [1/2]

void WriteArray ( const char * name,
const std::vector< uint8_t > & value )
inline

Serialize bytes from vector.

It uses BytesWriterStrategy to serialize bytes. The strategy can be set by SetBytesStrategy function.

Parameters
namename of value (key)
valuevector of bytes

Referenced by JsonWriter::Write(), EmbeddingTemplate::WriteTo(), FaceTemplate::WriteTo(), ICSTemplate::WriteTo(), IrisTemplate::WriteTo(), ModalitiesICSTemplate::WriteTo(), and ModalitiesTemplate::WriteTo().

Here is the caller graph for this function:

◆ WriteArray() [2/2]

template<typename T>
void WriteArray ( const char * name,
const T & array )

Serialize array of elements that are suitable for range-based for loop.

Parameters
namename of value (key)
arrayarray suitable for range-based for loop.
Template Parameters
Ttype suitable for range-based for loop.

◆ WriteNull()

virtual void WriteNull ( const char * name)
pure virtual

Serialize null object.

Parameters
namename of value (key)

Implemented in JsonWriter.

Referenced by Write(), and Write().

Here is the caller graph for this function:

◆ WriteRoot()

virtual void WriteRoot ( Serializable & s)
pure virtual

Serialize serializable as root object.

Parameters
sclass that implements Serializable interface

Implemented in JsonWriter.


The documentation for this class was generated from the following file: