This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.txt
211 lines (162 loc) · 6.22 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
____ _ _ _ _ _
| _ \ ___ _ __ ___(_)___| |_ | \ | | ___| |_
| |_) / _ \ '__/ __| / __| __| | \| |/ _ \ __|
| __/ __/ | \__ \ \__ \ |_ _| |\ | __/ |_
|_| \___|_| |___/_|___/\__| (_)_| \_|\___|\__|
Persist is a .Net serializer/deserializer supporting XML, Json & Yaml formats
Nuget package available!: Install-Package Persist
Can Serialize/Deserialize:
- Arbitrary custom types, native types, anonymous types, enums, IDictionary, IList, IConvertible
- Multiply and possibly cyclical references to objects
- Polymorphic objects
- Using converters through TypeConverterAttribute
- External types using MetadataTypeAttribute to specify the properties or converter to use
- Evolving types : you can add or remove properties/fields with no problem
- Xml, Json, Yaml and any format you want because it is extensible
- To/From a generic Node object that you can add/remove attributes and children
- Using a PersistAttribute that allows you to:
- Change the member name
- Specify if serialize/deserialize as a reference
- Ignore a member
Features:
- Serialize and Deserialize any .Net object with Persist's powerful serializer
- Since the metadata for the type is created on the constructor
serialization and deserialization is really fast
- Persist is open source so you can contribute and completely free for comercial use
- Create, parse, query and modify archives using Persist's Node and NodeAttribute objects
- Serialization of objects with references is clearer and smaller than Newtonsoft Json.net library
- Easy to use, comes with a static ArchiveUtils class with a lot of helper methods
- if you need it Persist supports conversion between all of the 3 data formats ( xml, yaml, json)
public class Movie
{
public enum Genre { Action, Comedy, Terror, Thriller, Romantic, Sfi, Boring }
public string Name { get; set; }
public DateTime ReleaseDate { get; set; }
public List<Genre> Genres { get; }
public static Movie BadBoys => new Movie
{
Name = "Bad boys",
ReleaseDate = DateTime.Now,
Genres = new List<Genre> { Genre.Action, Genre.Comedy }
};
}
ArchiveUtils.Write("BadBoys.yaml", Movie.BadBoys);
ArchiveUtils.Write("BadBoys.json", Movie.BadBoys, ArchiveFormat.Guess);
ArchiveUtils.Write("BadBoys.movie", Movie.BadBoys, ArchiveFormat.Xml);
Yaml:
Name: Bad boys
ReleaseDate: 05/30/2016 01:08:14
Genres:
- value: Action
- value: Comedy
...
Json:
{
"Name": "Bad boys",
"ReleaseDate": "05/30/2016 01:08:13",
"Genres": [
{
"value": "Action"
},
{
"value": "Comedy"
}
]
}
Xml:
<Movie Name="Bad boys" ReleaseDate="05/30/2016 00:55:49">
<Genres>
<Genre value="Action" />
<Genre value="Comedy" />
</Genres>
</Movie>
var newMovie = ArchiveUtils.Read<Movie>("BadBoys.json");
Additional Info:
- Persist serializes by default public properties, if you want it to also serialize private
properties or fields they must specify the PersistAttribute in the declaration of the
member of the class or metadata class
- When deserializing if a member has an instance that is already created it
will use that instance
- Persist by default will run the parameterless constructor when deserializing objects,
if you dont want this behaviour set the PersistAttribute.RunContructor
of the member to false.
- the XmlArchive only reads attributes and child nodes with more attributes and
child nodes, meaning this is invalid : <node>text</node>
- if you use a PersistAttribute like [Persist("")] on List & Dictionary members
using XmlArchive the container node will be ignored
-The metadata types are looked in the same Assembly as the main type.
Backwards Compatibility:
//BadBoys.movie
<Movie Name="Bad boys" ReleaseDate="05/30/2016 01:08:14">
<Genres>
<GENRE value="Action" />
<GENRE value="Comedy" />
</Genres>
</Movie>
Add this attribute:
[Persist(ChildName = "GENRE")]
public List<Genre> Genres { get; set; }
And there you have it:
var movie = ArchiveUtils.Read<Movie>("BadBoys.movie");
LINQ to Archive:
<Movie Name="Bad boys" ReleaseDate="05/30/2016 01:08:14">
<Genres>
<GENRE value="Genre_Action" />
<GENRE value="Genre_Comedy" />
</Genres>
</Movie>
var nodeToPatch = ArchiveUtils.LoadNode("BadBoys.movie");
nodeToPatch.Nodes.Single(n => n.Name == "Genres").Nodes.ForEach(n =>
{
var attr = n.Attributes.Single(a => a.Name == "value");
attr.Value = attr.Value.Replace("Genre_", "");
} );
var patchedMovie = ArchiveUtils.Read<Movie>(nodeToPatch);
Serializing using Persist attributes:
//Archive model
public class Classroom
{
[Persist("",ChildName = "Student")]
public List<Student> Students { get; set; }
[Persist("ClassroomRect")]
public System.Windows.Rect RandomExternalTypeYouWantToSerialize;
}
public class Student
{
[Persist("",IsReference = true, ChildName = "Friend")]
private readonly List<Student> m_friends = new List<Student>;
public string Name { get; set; }
[Persist(Ignore = true)]
public string StudentId {get; set; }
}
[System.ComponentModel.DataAnnotations.MetadataType(typeof(Rect))]
public class RectMeta
{
public double X { get; set; }
public double Y { get; set; }
[Persist("WIDTH")]
public double Width { get; set; }
public double Height { get; set; }
}
//Archive xml
<Classroom>
<ClassroomRect X="15" Y="12" WIDTH="99" Height="45" />
<Student Name="Alfred" id="4">
<Friend id="1" />
<Friend id="2" />
</Student>
<Student Name="Ben" id="1">
<Friend id="4" />
<Friend id="5" />
</Student>
<Student Name="Camila" id="2">
<Friend id="4" />
<Friend id="1" />
<Friend id="5" />
</Student>
<Student Name="Denise" id="5">
<Friend id="2" />
</Student>
</Classroom>
var archive = new XmlArchive(typeof(Classroom));
var newclassRoom = archive.Read("classroom.xml");