Skip to content

Quick Start

samatrhea edited this page Oct 15, 2022 · 5 revisions

Deserialization

An existing ReqIF document can be deserialized to an object graph using the ReqIFDeserializer class that implements the IReqIFDeserializer interface. The IReqIFDeserializer interface exposes 4 Deserialze methods:

IEnumerable<ReqIF> Deserialize(string fileUri, bool validate = false, ValidationEventHandler validationEventHandler = null); 

IEnumerable<ReqIF> Deserialize(Stream stream, SupportedFileExtensionKind fileExtensionKind, bool validate = false, ValidationEventHandler validationEventHandler = null);

Task<IEnumerable<ReqIF>> DeserializeAsync(string fileUri, CancellationToken token, bool validate = false, ValidationEventHandler validationEventHandler = null);

Task<IEnumerable<ReqIF>> DeserializeAsync(Stream stream, SupportedFileExtensionKind fileExtensionKind, CancellationToken token, bool validate = false, ValidationEventHandler validationEventHandler = null);

The Deserialize method returns an IEnumerable<ReqIF>. The ReqIF object that is the root node of a ReqIF document. The following code snippet demonstrates how to deserialize a ReqIF document without XML schema validation.

public void Deserialize()
{
    var path = "some-file-path.reqif";                        
    var deserializer = new ReqIFDeserializer();
    var reqif = deserializer.Deserialize(path).First();
    var header = reqif.TheHeader;
    var content = reqif.CoreContent;
}

The following code snippet demonstrates how to deserialize using XML schema validation.

public void Deserialize()
{
    var path = "some-file-path.reqif";                        
    var deserializer = new ReqIFDeserializer();
    var reqif = deserializer.Deserialize(path, true, this.ValidationEventHandler).First();;
    var header = reqif.TheHeader;
    var content = reqif.CoreContent;
}

public void ValidationEventHandler(object sender, ValidationEventArgs validationEventArgs)
{
    throw validationEventArgs.Exception;
}

NOTE: The deserializer supports the following input file extenions: .reqif, .reqifz, .zip

Serialization

The ReqIFSerializer class that implements the IReqIFSerializer interface is used to serialize a requirements specification to a reqif document. A ReqIF object, and it's content, can be constructed using the ReqIFSharp library. The IReqIFSerializer interface exposes 4 Deserialze methods:

void Serialize(IEnumerable<ReqIF> reqIfs, string fileUri);

void Serialize(IEnumerable<ReqIF> reqifs, Stream stream, SupportedFileExtensionKind fileExtensionKind);

Task SerializeAsync(IEnumerable<ReqIF> reqIfs, string fileUri, CancellationToken token);

Task SerializeAsync(IEnumerable<ReqIF> reqIfs, Stream stream, SupportedFileExtensionKind fileExtensionKind, CancellationToken token);

The following snippet demonstrates how serialization to a .reqif file can be achieved:

public void Serialize()
{
    var reqif = new ReqIF();
    reqIF.Lang = "en";

    var header = new ReqIFHeader()
    {
        Comment = "this is a comment",
        CreationTime = DateTime.Parse("2015-12-01"),
        Identifier = "reqifheader",
        RepositoryId = "a repos id",
        ReqIFToolId = "reqifsharp",
        ReqIFVersion = "1.0",
        SourceToolId = "reqifsharp",
        Title = "this is a title"
    };

    reqIF.TheHeader = header;

    var reqIfContent = new ReqIFContent();
    reqIF.CoreContent = reqIfContent;

    var reqifs = new List<ReqIF> { reqIF };

    var resultFileUri = "some-file-path.reqif";
    var serializer = new ReqIFSerializer();
    serializer.Serialize(reqifs, resultFileUri);
}

NOTE: The serializer supports the following output file extenions: .reqif, .reqifz, .zip

Clone this wiki locally