Skip to content

Commit 65e5683

Browse files
committed
Various documentation improvements.
- Starter on deserializing arrays. - TypeCacheFlag - TypeCache option. - StdClassOption examples and explanation. - Deserilzing: Add some examples for explicitely typed deserialization.
1 parent 30bf403 commit 65e5683

File tree

12 files changed

+349
-35
lines changed

12 files changed

+349
-35
lines changed

docs/Attributes/PhpClass.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
4+
5+
**Table of contents**
6+
1. [PhpClass](#PhpClass)
7+
1. [Example 1: Serialize with classname](#Example-1:-Serialize-with-classname)
8+
2. [Example 2: Serialize without classname](#Example-2:-Serialize-without-classname)
9+
3. [Example 2: Deserialize with classname](#Example-3:-Deserialize-with-classname)
10+
11+
---
12+
413
# PhpClass
514

615
Indicates that instances of the decorated class or struct should be serialized with Object notation and with which classname (if specified)

docs/Attributes/PhpIgnore.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11

2-
[back to overview](../Index.md)
2+
[back to overview](../README.md)
3+
4+
---
5+
6+
**Table of contents**
7+
1. [PhpIgnore](#PhpClass)
8+
1. [Example](#Example)
39

410
---
511

612
# PhpIgnore
713

8-
Tells both the serializer and deserializer to ignore a given property.
14+
Tells both the serializer and deserializer to ignore a given property.
915

1016
When deserializing, the property or field will assume the default value.
1117

docs/Attributes/PhpProperty.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
4+
5+
**Table of contents**
6+
1. [PhpProperty](#PhpProperty)
7+
1. [Example 1: Serialize](#Example-1:-Serialize)
8+
2. [Example 2: Deserialize with classname](#Example-1:-Deserialize-with-classname)
9+
3. [Example 3: Deserialization with actual name.](#Example-1:-Deserialize-with-actual-name)
10+
11+
---
12+
413
# PhpProperty
514

615
Specifiy the name of the property in the serialization data, for both serialization and deserialization.
716

8-
Please be aware that the actual name of the property may be used as a fallback for assignment. See example 3.
17+
Please be aware that the actual name of the property may be used as a fallback for assignment. See [example 3](#Example-1:-Deserialize-with-actual-name).
918

10-
## Example - Serialize
19+
## Example 1: Serialize
1120

1221

1322
```C#

docs/Options/PhpDeserializationOptions.md

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
44

@@ -12,14 +12,19 @@
1212
6. [InputEncoding](#InputEncoding)
1313
7. [StdClass](#StdClass)
1414
8. [EnableTypeLookup](#EnableTypeLookup)
15-
2. [ListOptions](#ListOptions)
15+
9. [TypeCache][#TypeCache]
16+
2. [ListOptions](#ListOptions)
1617
1. [Default](#Default)
1718
2. [OnAllIntegerKeys](#OnAllIntegerKeys)
1819
3. [Never](#Never)
1920
3. [StdClassOption](#StdClassOption)
2021
1. [Dictionary](#Dictionary)
21-
1. [Dynamic](#Dynamic)
22-
1. [Throw](#Throw)
22+
2. [Dynamic](#Dynamic)
23+
3. [Throw](#Throw)
24+
4. [TypeCacheFlag][#TypeCacheFlag]
25+
1. [Deactivated](#Deactivated)
26+
2. [ClassNames (default)](#ClassNames-(default))
27+
3. [PropertyInfo](#PropertyInfo)
2328

2429
---
2530

@@ -29,7 +34,7 @@ Options for deserializing PHP data.
2934

3035
## CaseSensitiveProperties
3136

32-
**Description:** Whether or not properties are matched case sensitive.
37+
**Description:** Whether or not properties are matched case sensitive.
3338

3439
**Default value:** `true`
3540

@@ -43,7 +48,7 @@ public class ExampleClass {
4348

4449
var deserialized = PhpSerialization.Deserialize<ExampleClass>(
4550
"a:2:{s:3:\"foo\";s:3:\"abc\";s:3:\"bar\";s:3:\"xyz\";}",
46-
new PhpDeserializationOptions() {
51+
new PhpDeserializationOptions() {
4752
CaseSensitiveProperties = true,
4853
AllowExcessKeys = true // To avoid exception, see below.
4954
}
@@ -63,8 +68,8 @@ public class ExampleClass {
6368

6469
var deserialized = PhpSerialization.Deserialize<ExampleClass>(
6570
"a:2:{s:3:\"foo\";s:3:\"abc\";s:3:\"bar\";s:3:\"xyz\";}",
66-
new PhpDeserializationOptions() {
67-
CaseSensitiveProperties = false,
71+
new PhpDeserializationOptions() {
72+
CaseSensitiveProperties = false,
6873
AllowExcessKeys = true // To avoid exception, see below.
6974
}
7075
);
@@ -127,6 +132,8 @@ var deserialized = PhpSerialization.Deserialize<ExampleClass>(
127132

128133
**Default value:** `true`
129134

135+
**Examples:** **TODO**
136+
130137
## NumberStringToBool
131138

132139
**Description:** Whether or not to convert strings "1"` and "0" to boolean.
@@ -167,7 +174,7 @@ var deserialized = PhpSerialization.Deserialize(
167174
var deserializedBool = PhpSerialization.Deserialize<boolean>(
168175
"s:1:\"1\"",
169176
new PhpDeserializationOptions() {
170-
NumberStringToBool = false
177+
NumberStringToBool = true
171178
}
172179
);
173180
// deserializedBool == true
@@ -179,12 +186,12 @@ var deserializedBool = PhpSerialization.Deserialize<boolean>(
179186

180187
**Default value:** `System.Text.Encoding.UTF8`
181188

182-
**Example:**
189+
**Example:**
183190
[See the unit tests](https://github.com/StringEpsilon/PhpSerializerNET/blob/main/PhpSerializerNET.Test/Deserialize/Options/InputEncoding.cs)
184191

185192
## StdClass
186193

187-
**Description:** Target datatype for objects of type "stdClass".
194+
**Description:** Target datatype for objects of with classname "stdClass".
188195

189196
Note: This does not affect explicitly typed deserialization via ```PhpSerialization.Deserialize<T>()```
190197

@@ -194,8 +201,8 @@ Note: This does not affect explicitly typed deserialization via ```PhpSerializat
194201

195202
**Description:** Enable or disable lookup in currently loaded assemblies for target classes and structs to deserialize objects into. i.E. `o:8:"UserInfo":...` being mapped to a UserInfo class.
196203

197-
**Notes:**
198-
* Disabling this option can improve performance in some cases and reduce memory footprint. Finding a class or struct with a given name is a relatively costly operation and the library caches the results.
204+
**Notes:**
205+
* Disabling this option can improve performance in some cases and reduce memory footprint. Finding a class or struct with a given name is a relatively costly operation and the library caches the results.
199206
* The caching might lead to some unexpected behaviors too. If the library can not find a type for a given name, the negative result will be stored for future deserializations. If a new assembly with a matchign type is loaded in the meantime, the type lookup will still fail and the fallback option (see [PhpDeserializationOptions.StdClass](#StdClass)) will be used.
200207

201208
**Default value:** `true`
@@ -235,6 +242,10 @@ var deserialized = PhpSerialization.Deserialize(
235242
// See also the StdClass option.
236243
```
237244

245+
## TypeCache
246+
247+
Controls what type information cached. See [TypeCacheFlag](#TypeCacheFlag) for details.
248+
238249
# ListOptions
239250

240251
Available values for [PhpDeserializationOptions.UseLists](#UseLists).
@@ -243,9 +254,9 @@ Available values for [PhpDeserializationOptions.UseLists](#UseLists).
243254

244255
Convert associative array to list when all keys are consecutive integers. Otherwise make a dictionary.
245256

246-
**Notes:**
257+
**Notes:**
247258
* This means `0, 1, 2, 3, 4`, but also `9, 10, 11, 12`. The library does not check that the indizes start at 0.
248-
* The consecutiveness is only checked in the positive direction.
259+
* The consecutiveness is only checked in the positive direction.
249260

250261
**Example**
251262

@@ -299,10 +310,93 @@ Available values for [PhpDeserializationOptions.StdClass](#StdClass).
299310

300311
Deserialize all 'stdClass' objects into `PhpObjectDictionary` (extending `Dictionary<string, object>`).
301312

313+
This is the default option.
314+
315+
```c#
316+
var objectDictionary = (PhpObjectDictionary)PhpSerialization.Deserialize(
317+
"O:8:\"stdClass\":2:{s:3:\"Foo\";s:3:\"xyz!\";s:3:\"Bar\";d:3.1415}",
318+
new PhpDeserializationOptions() {
319+
StdClass = StdClassOption.Dictionary,
320+
}
321+
);
322+
/*
323+
objectDictionary["Foo"] == "xyz"
324+
objectDictionary["Bar"] == 3.1415
325+
*/
326+
```
327+
302328
## Dynamic
303329

304-
Deserialize all 'stdClass' objects into dynamic objects (see `PhpDynamicObject`).
330+
Deserialize all 'stdClass' objects into dynamic objects. See [System.Dynamic.DynamicObject](https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject?view=net-6.0) for more details on dynamic objects in general.
331+
332+
The option will result in instances of [PhpDynamicObject](../Types/PhpDynamicObject.md) specifically.
333+
334+
```c#
335+
dynamic object = PhpSerialization.Deserialize(
336+
"O:8:\"stdClass\":2:{s:3:\"Foo\";s:3:\"xyz!\";s:3:\"Bar\";d:3.1415}",
337+
new PhpDeserializationOptions() {
338+
StdClass = StdClassOption.Dynamic,
339+
}
340+
);
341+
/*
342+
object.Foo == "xyz"
343+
object.Bar == 3.1415
344+
*/
345+
```
305346

306347
## Throw
307348

308-
Throw an exception and abort deserialization when encountering stdClass objects.
349+
Throw an exception and abort deserialization when encountering 'stdClass' objects.
350+
351+
```c#
352+
try {
353+
_ = PhpSerialization.Deserialize(
354+
"O:8:\"stdClass\":2:{s:3:\"Foo\";s:3:\"xyz!\";s:3:\"Bar\";d:3.1415}",
355+
new PhpDeserializationOptions() {
356+
StdClass = StdClassOption.Throw,
357+
}
358+
);
359+
} catch (DeserializationException ex) {
360+
// ex.Message = "Encountered 'stdClass' and the behavior 'Throw' was specified in deserialization options."
361+
}
362+
```
363+
364+
# TypeCacheFlag
365+
366+
Specifies the behavior of the type information caches of the library.
367+
368+
Note that these are flags. To combine them, use the `|` operator:
369+
```c#
370+
TypeCache = TypeCacheFlag.ClassNames | TypeCacheFlag.PropertyInfo
371+
```
372+
373+
The `Deactivated` can not be combined. The other flags will override it:
374+
375+
```c#
376+
// don't:
377+
TypeCache = TypeCacheFlag.Deactivated | TypeCacheFlag.PropertyInfo
378+
379+
// do:
380+
TypeCache = TypeCacheFlag.Deactivated
381+
```
382+
383+
## Deactivated
384+
385+
Do not cache anything.
386+
**Beware:** This can cause severe performance degradation when dealing with lots of the same Objects in the data to deserialize.
387+
388+
In a simple test of deserializing the same object data in a loop, I saw a **400 fold** increase in run time when disabling all caching.
389+
390+
## ClassNames (default)
391+
392+
Enable or disable lookup in currently loaded assemblies for target classes and structs to deserialize objects into.
393+
i.E. `o:8:"UserInfo":...` being mapped to a UserInfo class.
394+
Note: This does not affect use of PhpSerialization.Deserialize<T>()
395+
396+
## PropertyInfo
397+
398+
Enable or disable cache for property information of classes and structs that are handled during deserialization.
399+
This can speed up work signifcantly when dealing with a lot of instances of those types but might decrease performance when dealing with
400+
lots of structures or only deserializing a couple instances.
401+
402+
In the same test as with the `Deactivated` option, I saw roughly 0.5x the run time.

docs/Options/PhpSerializiationOptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
44

docs/Index.md renamed to docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Usage
44

55
* [Deserializing data](./Usage/Deserializing.md)
6-
* [Deserializing data](./Usage/Serializing.md)
6+
* [Serializing data](./Usage/Serializing.md)
77

88
## API
99

docs/Types/IPhpObject.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
44

docs/Types/PhpDateTime.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
44

55
# Class PhpDateTime
66

7-
POCO for the PHP `DateTime` class.
7+
POCO for the PHP `DateTime` class.
88

9-
PHP documentation is available here: https://www.php.net/manual/en/class.datetime.php
9+
PHP documentation is available here: https://www.php.net/manual/en/class.datetime.php
1010

1111
## Constructor
1212

docs/Types/PhpDynamicObject.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
44

docs/Types/PhpObjectDictionary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[back to overview](../Index.md)
1+
[back to overview](../README.md)
22

33
---
44

0 commit comments

Comments
 (0)