2011-10-11 13:58:23 +00:00
|
|
|
|
|
|
|
// Copyright Ferdinand Majerech 2011.
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* YAML serializer.
|
|
|
|
* Code based on PyYAML: http://www.pyyaml.org
|
|
|
|
*/
|
|
|
|
module dyaml.serializer;
|
|
|
|
|
|
|
|
|
|
|
|
import std.array;
|
|
|
|
import std.format;
|
2011-10-23 18:17:37 +00:00
|
|
|
import std.typecons;
|
2011-10-11 13:58:23 +00:00
|
|
|
|
|
|
|
import dyaml.anchor;
|
|
|
|
import dyaml.emitter;
|
|
|
|
import dyaml.encoding;
|
|
|
|
import dyaml.event;
|
|
|
|
import dyaml.exception;
|
|
|
|
import dyaml.node;
|
|
|
|
import dyaml.resolver;
|
|
|
|
import dyaml.tag;
|
2011-11-16 02:10:29 +00:00
|
|
|
import dyaml.tagdirective;
|
2011-10-11 13:58:23 +00:00
|
|
|
import dyaml.token;
|
|
|
|
|
|
|
|
|
|
|
|
package:
|
|
|
|
|
|
|
|
///Serializes represented YAML nodes, generating events which are then emitted by Emitter.
|
|
|
|
struct Serializer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
///Emitter to emit events produced.
|
|
|
|
Emitter* emitter_;
|
|
|
|
///Resolver used to determine which tags are automaticaly resolvable.
|
|
|
|
Resolver resolver_;
|
|
|
|
|
|
|
|
///Do all document starts have to be specified explicitly?
|
2012-09-13 23:21:01 +00:00
|
|
|
Flag!"explicitStart" explicitStart_;
|
2011-10-11 13:58:23 +00:00
|
|
|
///Do all document ends have to be specified explicitly?
|
2012-09-13 23:21:01 +00:00
|
|
|
Flag!"explicitEnd" explicitEnd_;
|
2011-10-11 13:58:23 +00:00
|
|
|
///YAML version string.
|
|
|
|
string YAMLVersion_;
|
|
|
|
|
|
|
|
///Tag directives to emit.
|
2011-11-16 02:10:29 +00:00
|
|
|
TagDirective[] tagDirectives_;
|
2011-10-11 13:58:23 +00:00
|
|
|
|
|
|
|
//TODO Use something with more deterministic memory usage.
|
|
|
|
///Nodes with assigned anchors.
|
|
|
|
Anchor[Node] anchors_;
|
|
|
|
///Nodes with assigned anchors that are already serialized.
|
|
|
|
bool[Node] serializedNodes_;
|
|
|
|
///ID of the last anchor generated.
|
|
|
|
uint lastAnchorID_ = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a Serializer.
|
|
|
|
*
|
|
|
|
* Params: emitter = Emitter to emit events produced.
|
|
|
|
* resolver = Resolver used to determine which tags are automaticaly resolvable.
|
|
|
|
* encoding = Character encoding to use.
|
|
|
|
* explicitStart = Do all document starts have to be specified explicitly?
|
|
|
|
* explicitEnd = Do all document ends have to be specified explicitly?
|
|
|
|
* YAMLVersion = YAML version string.
|
|
|
|
* tagDirectives = Tag directives to emit.
|
|
|
|
*/
|
|
|
|
this(ref Emitter emitter, Resolver resolver, Encoding encoding,
|
2012-09-13 23:21:01 +00:00
|
|
|
const Flag!"explicitStart" explicitStart,
|
|
|
|
const Flag!"explicitEnd" explicitEnd, string YAMLVersion,
|
2012-09-08 23:42:13 +00:00
|
|
|
TagDirective[] tagDirectives) @trusted
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
2012-09-13 23:21:01 +00:00
|
|
|
emitter_ = &emitter;
|
|
|
|
resolver_ = resolver;
|
2011-10-11 13:58:23 +00:00
|
|
|
explicitStart_ = explicitStart;
|
2012-09-13 23:21:01 +00:00
|
|
|
explicitEnd_ = explicitEnd;
|
|
|
|
YAMLVersion_ = YAMLVersion;
|
2011-10-11 13:58:23 +00:00
|
|
|
tagDirectives_ = tagDirectives;
|
|
|
|
|
|
|
|
emitter_.emit(streamStartEvent(Mark(), Mark(), encoding));
|
|
|
|
}
|
|
|
|
|
|
|
|
///Destroy the Serializer.
|
2012-09-08 23:42:13 +00:00
|
|
|
@safe ~this()
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
|
|
|
emitter_.emit(streamEndEvent(Mark(), Mark()));
|
|
|
|
clear(YAMLVersion_);
|
|
|
|
YAMLVersion_ = null;
|
|
|
|
clear(serializedNodes_);
|
|
|
|
serializedNodes_ = null;
|
|
|
|
clear(anchors_);
|
|
|
|
anchors_ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Serialize a node, emitting it in the process.
|
2012-09-08 23:42:13 +00:00
|
|
|
void serialize(ref Node node) @safe
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
|
|
|
emitter_.emit(documentStartEvent(Mark(), Mark(), explicitStart_,
|
|
|
|
YAMLVersion_, tagDirectives_));
|
|
|
|
anchorNode(node);
|
|
|
|
serializeNode(node);
|
|
|
|
emitter_.emit(documentEndEvent(Mark(), Mark(), explicitEnd_));
|
|
|
|
clear(serializedNodes_);
|
|
|
|
clear(anchors_);
|
|
|
|
Anchor[Node] emptyAnchors;
|
|
|
|
anchors_ = emptyAnchors;
|
|
|
|
lastAnchorID_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Determine if it's a good idea to add an anchor to a node.
|
|
|
|
*
|
|
|
|
* Used to prevent associating every single repeating scalar with an
|
|
|
|
* anchor/alias - only nodes long enough can use anchors.
|
|
|
|
*
|
|
|
|
* Params: node = Node to check for anchorability.
|
|
|
|
*
|
|
|
|
* Returns: True if the node is anchorable, false otherwise.
|
|
|
|
*/
|
2012-09-08 23:42:13 +00:00
|
|
|
static bool anchorable(ref Node node) @safe
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
|
|
|
if(node.isScalar)
|
|
|
|
{
|
2011-10-22 15:06:32 +00:00
|
|
|
return node.isType!string ? node.as!string.length > 64 :
|
|
|
|
node.isType!(ubyte[]) ? node.as!(ubyte[]).length > 64:
|
2011-10-11 13:58:23 +00:00
|
|
|
false;
|
|
|
|
}
|
|
|
|
return node.length > 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Add an anchor to the node if it's anchorable and not anchored yet.
|
2012-09-08 23:42:13 +00:00
|
|
|
void anchorNode(ref Node node) @safe
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
|
|
|
if(!anchorable(node)){return;}
|
|
|
|
|
|
|
|
if((node in anchors_) !is null)
|
|
|
|
{
|
|
|
|
if(anchors_[node].isNull())
|
|
|
|
{
|
|
|
|
anchors_[node] = generateAnchor();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
anchors_[node] = Anchor(null);
|
2011-10-26 19:05:56 +00:00
|
|
|
if(node.isSequence) foreach(ref Node item; node)
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
2011-10-26 19:05:56 +00:00
|
|
|
anchorNode(item);
|
2011-10-11 13:58:23 +00:00
|
|
|
}
|
2011-10-26 19:05:56 +00:00
|
|
|
else if(node.isMapping) foreach(ref Node key, ref Node value; node)
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
2011-10-26 19:05:56 +00:00
|
|
|
anchorNode(key);
|
|
|
|
anchorNode(value);
|
2011-10-11 13:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///Generate and return a new anchor.
|
2012-09-08 23:42:13 +00:00
|
|
|
Anchor generateAnchor() @trusted
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
|
|
|
++lastAnchorID_;
|
2013-04-23 23:32:16 +00:00
|
|
|
auto appender = appender!string();
|
2011-10-11 13:58:23 +00:00
|
|
|
formattedWrite(appender, "id%03d", lastAnchorID_);
|
|
|
|
return Anchor(appender.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
///Serialize a node and all its subnodes.
|
2012-09-08 23:42:13 +00:00
|
|
|
void serializeNode(ref Node node) @trusted
|
2011-10-11 13:58:23 +00:00
|
|
|
{
|
|
|
|
//If the node has an anchor, emit an anchor (as aliasEvent) on the
|
|
|
|
//first occurrence, save it in serializedNodes_, and emit an alias
|
|
|
|
//if it reappears.
|
|
|
|
Anchor aliased = Anchor(null);
|
|
|
|
if(anchorable(node) && (node in anchors_) !is null)
|
|
|
|
{
|
|
|
|
aliased = anchors_[node];
|
|
|
|
if((node in serializedNodes_) !is null)
|
|
|
|
{
|
|
|
|
emitter_.emit(aliasEvent(Mark(), Mark(), aliased));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serializedNodes_[node] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(node.isScalar)
|
|
|
|
{
|
|
|
|
assert(node.isType!string, "Scalar node type must be string before serialized");
|
2011-10-22 15:06:32 +00:00
|
|
|
auto value = node.as!string;
|
2011-10-25 18:23:44 +00:00
|
|
|
const detectedTag = resolver_.resolve(NodeID.Scalar, Tag(null), value, true);
|
|
|
|
const defaultTag = resolver_.resolve(NodeID.Scalar, Tag(null), value, false);
|
2011-10-23 18:17:37 +00:00
|
|
|
bool isDetected = node.tag_ == detectedTag;
|
|
|
|
bool isDefault = node.tag_ == defaultTag;
|
2011-10-11 13:58:23 +00:00
|
|
|
|
2011-10-20 13:48:21 +00:00
|
|
|
emitter_.emit(scalarEvent(Mark(), Mark(), aliased, node.tag_,
|
2011-10-27 21:13:14 +00:00
|
|
|
tuple(isDetected, isDefault), value, node.scalarStyle));
|
2011-10-11 13:58:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(node.isSequence)
|
|
|
|
{
|
2011-10-26 19:05:56 +00:00
|
|
|
const defaultTag = resolver_.defaultSequenceTag;
|
2011-10-30 19:24:43 +00:00
|
|
|
const implicit = node.tag_ == defaultTag;
|
2011-10-20 13:48:21 +00:00
|
|
|
emitter_.emit(sequenceStartEvent(Mark(), Mark(), aliased, node.tag_,
|
2011-10-27 21:13:14 +00:00
|
|
|
implicit, node.collectionStyle));
|
2011-10-11 13:58:23 +00:00
|
|
|
foreach(ref Node item; node)
|
|
|
|
{
|
|
|
|
serializeNode(item);
|
|
|
|
}
|
|
|
|
emitter_.emit(sequenceEndEvent(Mark(), Mark()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(node.isMapping)
|
|
|
|
{
|
2011-10-30 19:24:43 +00:00
|
|
|
const defaultTag = resolver_.defaultMappingTag;
|
|
|
|
const implicit = node.tag_ == defaultTag;
|
2011-10-20 13:48:21 +00:00
|
|
|
emitter_.emit(mappingStartEvent(Mark(), Mark(), aliased, node.tag_,
|
2011-10-27 21:13:14 +00:00
|
|
|
implicit, node.collectionStyle));
|
2011-10-11 13:58:23 +00:00
|
|
|
foreach(ref Node key, ref Node value; node)
|
|
|
|
{
|
|
|
|
serializeNode(key);
|
|
|
|
serializeNode(value);
|
|
|
|
}
|
|
|
|
emitter_.emit(mappingEndEvent(Mark(), Mark()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(false, "This code should never be reached");
|
|
|
|
}
|
|
|
|
}
|