Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Heromyth committed Apr 8, 2021
1 parent de31ced commit 1a9286d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 94 deletions.
138 changes: 44 additions & 94 deletions examples/Simple/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@ import hunt.xml;
import std.stdio;

void main() {
//readAndWrite();
//loadAndSave();
//objectToXml();
xmlToObject2();
// testStruct();
// readAndWrite();
// loadAndSave();
// objectToXml();
// xmlToObject();
// testStruct();
escapeTest();
}

void escapeTest() {
string text = `
<receiverInfo>
<detailAddress>602&amp;#35;.</detailAddress>
<detailAddress>602&#35;.</detailAddress>
</receiverInfo>
`;

Document doc = Document.parse(text);

writeln(doc.toPrettyString());

}

Expand All @@ -15,38 +29,38 @@ void readAndWrite() {
Document doc = Document.parse("<single-element attr1='one' attr2=\"two\"/>");
auto node = doc.firstNode();
assert(node.getName() == "single-element");
assert(doc.toPrettyString() == "<single-element attr1='one' attr2='two'/>\n");
assert(doc.toPrettyString() == "<single-element attr1='one' attr2='two'/>\n");
}

// load/save
void loadAndSave() {
Document document = Document.load("resources/books.xml");
document.save("output.xml");
Document document = Document.load("resources/books.xml");
document.save("output.xml");
}

// Serialization
void objectToXml() {
GreetingSettings settings = new GreetingSettings(1002, "Red");
settings.name = "hunt";
GreetingSettings settings = new GreetingSettings(1002, "Red");
settings.name = "hunt";

Document doc = toDocument(settings);
writeln(doc.toPrettyString());
Document doc = toDocument(settings);
writeln(doc.toPrettyString());

/*
/*
<Greeting ID='1002'>
<GreetingBase>
<city/>
<name>hunt</name>
</GreetingBase>
<Color>Red</Color>
</Greeting>
*/
*/
}


// Deserialization
void xmlToObject() {
string text = `
<?xml version="1.0" encoding="utf-8"?>
string text = `
<Greeting ID='1003'>
<GreetingBase>
<city/>
Expand All @@ -56,79 +70,15 @@ void xmlToObject() {
</Greeting>
`;

auto obj = toObject!(GreetingSettings)(text);
assert(obj._id == 1003);
assert(obj.name == "hunt");
assert(obj.color == "Red");
auto obj = toObject!(GreetingSettings)(text);
assert(obj._id == 1003);
assert(obj.name == "hunt");
assert(obj.color == "Red");
}

void xmlToObject2() {
string text = `
<response>
<flag>success|failure</flag>
<code>响应码</code>
<message>响应信息</message>
<item>
<itemCode>商品编码, string (50) , 必填</itemCode>
<extendProps>
<key1>value1</key1>
<key2>value2</key2>
</extendProps>
</item>
</response>
`;

auto obj = toObject!(Test)(text);
assert(obj.flag == "success|failure");
assert(obj.code == "响应码");
assert(obj.message == "响应信息");
assert(obj.item !is null);
assert(obj.item.itemCode == "商品编码, string (50) , 必填");
assert(obj.item.extendProps.key1 == "value1");
assert(obj.item.extendProps.key2 == "value2");

Document doc = toDocument(obj);
writeln(doc.toPrettyString());
}

class Item {
this() {
}

@XmlElement("itemCode")
string itemCode;

@XmlElement("extendProps")
Extend extendProps;
}

class Extend {
@XmlElement("key1")
string key1;

@XmlElement("key2")
string key2;
}

@XmlRootElement("response")
class Test {

@XmlElement("flag")
string flag;

@XmlElement("code")
string code;

@XmlElement("message")
string message;

@XmlElement("item")
Item item;
}

/**
*
/**
*
*/
@XmlRootElement("GreetingBase")
abstract class GreetingSettingsBase {
Expand All @@ -144,13 +94,13 @@ class GreetingSettings : GreetingSettingsBase {

@XmlElement("Color")
private string _color;

this() {
_color = "black";
}

this(int id, string color) {
_id = id;
_id = id;
_color = color;
}

Expand All @@ -164,14 +114,14 @@ class GreetingSettings : GreetingSettingsBase {
}

void testStruct() {
Vector3 vector = Vector3(1, 3, 5);
Vector3 vector = Vector3(1,3,5);

Document doc = toDocument(vector);
writeln(doc.toPrettyString());
Document doc = toDocument(vector);
writeln(doc.toPrettyString());

Vector3 v3 = toObject!(Vector3)(doc);
Vector3 v3 = toObject!(Vector3)(doc);
}

struct Vector3 {
float x, y, z;
}
float x, y, z;
}
2 changes: 2 additions & 0 deletions source/hunt/xml/Internal.d
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ static char[] skipAndExpandCharacterRefs(T , TP , int Flags)(ref char[] text)
dest[index] = ('&');
++index;
src=src[5..$];

continue;
}
if (src[2] == ('p') && src[3] == ('o') && src[4] == ('s') && src[5] == (';'))
Expand Down Expand Up @@ -486,6 +487,7 @@ static char[] skipAndExpandCharacterRefs(T , TP , int Flags)(ref char[] text)

// Return new end
text = src;
dest = dest[index..$];
return dest;

}
Expand Down

0 comments on commit 1a9286d

Please sign in to comment.