Skip to content

Commit 4c033a1

Browse files
committed
Deserialization: Very minor improvements for deserializing PhpDynamicObject and PhpObjectDictionary
1 parent bb661af commit 4c033a1

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

PhpSerializerNET/Deserialization/PhpDeserializer.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,34 +247,33 @@ int tokenPosition
247247

248248
private object MakeClass(in PhpToken token) {
249249
var typeName = this.GetString(token);
250-
object constructedObject;
251250
Type targetType = null;
252251
if (typeName != "stdClass" && this._options.EnableTypeLookup) {
253252
targetType = TypeLookup.FindTypeInAssymbly(typeName, this._options.TypeCache.HasFlag(TypeCacheFlag.ClassNames));
254253
}
255-
if (targetType != null && typeName != "stdClass") {
256-
_currentToken--; // go back one because we're basically re-entering the object-token from the top.
257-
// If we don't decrement the pointer, we'd start with the first child token instead of the object token.
258-
constructedObject = this.DeserializeToken(targetType);
259-
} else {
260-
dynamic result;
254+
if (targetType == null || typeName == "stdClass") {
261255
if (_options.StdClass == StdClassOption.Dynamic) {
262-
result = new PhpDynamicObject();
256+
var result = new PhpDynamicObject(token.Length, typeName);
257+
result.SetClassName(typeName);
258+
for (int i = 0; i < token.Length; i++) {
259+
result.TryAdd((string)this.DeserializeToken(), this.DeserializeToken());
260+
}
261+
return result;
263262
} else if (this._options.StdClass == StdClassOption.Dictionary) {
264-
result = new PhpObjectDictionary();
263+
var result = new PhpObjectDictionary(token.Length, typeName);
264+
result.SetClassName(typeName);
265+
for (int i = 0; i < token.Length; i++) {
266+
result.TryAdd((string)this.DeserializeToken(), this.DeserializeToken());
267+
}
268+
return result;
265269
} else {
266270
throw new DeserializationException("Encountered 'stdClass' and the behavior 'Throw' was specified in deserialization options.");
267271
}
268-
for (int i = 0; i < token.Length; i++) {
269-
var key = this.DeserializeToken();
270-
var value = this.DeserializeToken();
271-
result.TryAdd(
272-
(string)key,
273-
value
274-
);
275-
}
276-
constructedObject = result;
277272
}
273+
// go back one because we're basically re-entering the object-token from the top.
274+
// If we don't decrement the pointer, we'd start with the first child token instead of the object token.
275+
_currentToken--;
276+
var constructedObject = this.DeserializeToken(targetType);
278277
if (constructedObject is IPhpObject phpObject and not PhpDateTime) {
279278
phpObject.SetClassName(typeName);
280279
}

PhpSerializerNET/Types/PhpDynamicObject.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,30 @@ This Source Code Form is subject to the terms of the Mozilla Public
1010
namespace PhpSerializerNET;
1111

1212
public class PhpDynamicObject : DynamicObject, IPhpObject {
13-
private readonly PhpObjectDictionary _dictionary = new();
13+
private readonly PhpObjectDictionary _dictionary;
1414

15-
public PhpDynamicObject() { }
15+
public PhpDynamicObject() {
16+
_dictionary = new();
17+
}
18+
19+
public PhpDynamicObject(int capacity, string className) {
20+
_dictionary = new(capacity, className);
21+
}
1622

1723
public void SetClassName(string className) => this._dictionary.SetClassName(className);
24+
1825
public string GetClassName() => this._dictionary.GetClassName();
1926

2027
internal void TryAdd(string key, object value) => this._dictionary.TryAdd(key, value);
2128

22-
internal object GetMember(string name) {
23-
return this._dictionary[name];
24-
}
29+
internal object GetMember(string name) => this._dictionary[name];
2530

26-
public override ICollection<string> GetDynamicMemberNames() {
27-
return this._dictionary.Keys;
28-
}
31+
public override ICollection<string> GetDynamicMemberNames() => this._dictionary.Keys;
2932

3033
public override bool TryGetMember(GetMemberBinder binder, out object result) {
3134
return this._dictionary.TryGetValue(binder.Name, out result);
3235
}
36+
3337
public override bool TrySetMember(SetMemberBinder binder, object value) {
3438
this._dictionary[binder.Name] = value;
3539
return true;

PhpSerializerNET/Types/PhpObjectDictionary.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ namespace PhpSerializerNET;
1313
/// </summary>
1414
public class PhpObjectDictionary : Dictionary<string, object>, IPhpObject {
1515
private string _className;
16+
public PhpObjectDictionary() { }
1617

17-
public string GetClassName() {
18-
return this._className;
18+
public PhpObjectDictionary(int capacity, string className) : base(capacity) {
19+
this._className = className;
1920
}
2021

22+
public string GetClassName() => this._className;
23+
2124
public void SetClassName(string className) {
2225
this._className = className;
2326
}

0 commit comments

Comments
 (0)