Skip to content

Commit df0f5f5

Browse files
committed
Documentation for new mixed key / integer key capabilities.
1 parent 93be0d6 commit df0f5f5

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

PhpSerializerNET/Attributes/PhpProperty.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ public class PhpPropertyAttribute : Attribute {
1414
public long Key { get; set; }
1515
public bool IsInteger { get; private set; } = false;
1616

17+
/// <summary>
18+
/// Define the property key, as a string, for de/serialization.
19+
/// </summary>
1720
public PhpPropertyAttribute(string name) {
1821
this.Name = name;
1922
}
2023

2124
/// <summary>
22-
/// Define an integer key for a given property.
25+
/// Define an integer key for de/serialization.
2326
/// Note: This also affects serialization into object notation, as that is a legal way of representing an object.
2427
/// </summary>
28+
/// <remark>
29+
/// Deserialization of objects and arrays with mixed keys may yield unexpected results and or exceptions on certain target types.
30+
/// </remarks>
2531
public PhpPropertyAttribute(long key) {
2632
this.Key = key;
2733
this.IsInteger = true;

docs/Attributes/PhpProperty.md

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,40 @@
44

55
**Table of contents**
66
1. [PhpProperty](#PhpProperty)
7+
1. [Signatures](#signatures)
8+
1. [Examples](#Examples)
79
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+
2. [Example 2: Serialize with integer keys](#Example-1:-Serialize)
11+
3. [Example 3: Deserialize with classname](#Example-3:-Deserialize)
12+
4. [Example 4: Deserialization with actual name.](#Example-4:-Deserialization-with-actual-name)
13+
4. [Example 5: Deserialize with integer names](#Example-5:-Deserialize-with-integer-names)
1014

11-
---
1215

1316
# PhpProperty
1417

1518
Specifiy the name of the property in the serialization data, for both serialization and deserialization.
1619

1720
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).
1821

22+
## Signatures
23+
24+
You can use the PhpProperty attribute with both a string and an integer argument.
25+
26+
```cs
27+
[PhpProperty("string")]
28+
```
29+
30+
```cs
31+
[PhpProperty(42)]
32+
```
33+
34+
Indicating that the de/serialization should use the string and integer values as the key for the property respectively.
35+
36+
**Important**:
37+
Deserialization of objects and arrays with integer keys may yield unexpected results and or exceptions on certain target types. `PhpObjectDictionary` and `PhpDynamicObject` only supports string keys, for example, and will throw an exception.
38+
39+
# Examples
40+
1941
## Example 1: Serialize
2042

2143

@@ -34,8 +56,26 @@ var serialized = PhpSerialization.Serialize(
3456
// a:2:{s:3:"Foo";s:3:"abc";s:3:"Bar";s:3:"xyz";}
3557
```
3658

59+
## Example 1: Serialize with integer keys
60+
61+
62+
```C#
63+
public class ExampleClass {
64+
[PhpProperty1(0)]
65+
public string FirstElement { get; set; }
66+
[PhpProperty(1)]
67+
public string SecondElement { get; set; }
68+
}
69+
70+
var serialized = PhpSerialization.Serialize(
71+
new ExampleClass(){ FirstElement = "abc", SecondElement = "xyz"}
72+
);
73+
// "serialized" will be:
74+
// a:2:{i:0;s:3:"abc";i:1;s:3:"xyz";}
75+
```
76+
3777

38-
## Example 2: Deserialize with classname
78+
## Example 3: Deserialize
3979

4080
```C#
4181
public class ExampleClass {
@@ -53,21 +93,37 @@ var deserialized = PhpSerialization.Deserialize<ExampleClass>(
5393
// deserialized.SecondElement == "xyz"
5494
```
5595

56-
## Example 3: Deserialization with actual name.
96+
## Example 4: Deserialization with actual name
5797

5898
```C#
5999
public class ExampleClass {
60-
[PhpProperty("Foo")]
61100
public string FirstElement { get; set; }
62-
[PhpProperty("Bar")]
63101
public string SecondElement { get; set; }
64102
}
65103

66104
var deserialized = PhpSerialization.Deserialize<ExampleClass>(
67-
"a:2:{s:12:\"FirstElement\";s:13:\"abc\";s:3:\"SecondElement\";s:3:\"xyz\";}"
105+
"a:2:{s:12:\"FirstElement\";s:13:\"abc\";s:13:\"SecondElement\";s:3:\"xyz\";}"
68106
);
69107

70108
// The serialization uses the actual property names, so they will be used for assignment. Hence:
109+
// deserialized.FirstElement == "abc"
110+
// deserialized.SecondElement == "xyz"
111+
```
112+
113+
## Example 5: Deserialize with integer names
114+
115+
```C#
116+
public class ExampleClass {
117+
[PhpProperty(0)]
118+
public string FirstElement { get; set; }
119+
[PhpProperty(1)]
120+
public string SecondElement { get; set; }
121+
}
122+
123+
var deserialized = PhpSerialization.Deserialize<ExampleClass>(
124+
"a:2:{i:0;s:3:\"abc\";i:1;s:3:\"xyz\";}"
125+
);
126+
71127
// deserialized.FirstElement == "abc"
72128
// deserialized.SecondElement == "xyz"
73129
```

docs/Types/PhpDynamicObject.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Implements [`IPhpObject`](./IPhpObject.md).
99

1010
A dynamic object that can hold any property, used for deserializing object notation while providing a way to access the specified classname.
1111

12+
**Important**:
13+
14+
`PhpObjectDictionary` only supports string keys. You can not deserialize PHP objects with integer keys using this type. This may be addressed in future releases.
15+
1216
## Methods
1317

1418
### SetClassName
@@ -45,4 +49,5 @@ myObject.SetClassName("Person");
4549

4650
PhpSerialization.Serialize(myObject);
4751
// O:6:"Person":2:{s:9:"firstname";s:6:"Joseph";s:8:"lastname";s:6:"Bishop";}
48-
```
52+
```
53+

docs/Types/PhpObjectDictionary.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ objectDictionary.SetClassName("Person");
2121

2222
PhpSerialization.Serialize(myObject);
2323
// O:6:"Person":2:{s:9:"firstname";s:6:"Joseph";s:8:"lastname";s:6:"Bishop";}
24-
```
24+
```
25+
26+
**Important**:
27+
28+
`PhpObjectDictionary` only supports string keys. You can not deserialize PHP objects with integer keys using this type. This may be addressed in future releases.

0 commit comments

Comments
 (0)