-
Notifications
You must be signed in to change notification settings - Fork 188
fix: (.NET) Improve json De/serialization #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0a48f7a
b44f0b1
4c6b9f7
b071cbc
e9586d2
5ad9a4b
953600d
d9f6d71
cc2cd62
ebbad72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -467,7 +467,7 @@ public function getFilters(): array | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* get sub_scheme and property_name functions | ||||||||||||||||||||||||||||||||||
* get sub_scheme, property_name and parse_value functions | ||||||||||||||||||||||||||||||||||
* @return TwigFunction[] | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
public function getFunctions(): array | ||||||||||||||||||||||||||||||||||
|
@@ -494,7 +494,7 @@ public function getFunctions(): array | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return $result; | ||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||
}, ['is_safe' => ['html']]), | ||||||||||||||||||||||||||||||||||
new TwigFunction('property_name', function (array $definition, array $property) { | ||||||||||||||||||||||||||||||||||
$name = $property['name']; | ||||||||||||||||||||||||||||||||||
$name = \str_replace('$', '', $name); | ||||||||||||||||||||||||||||||||||
|
@@ -504,6 +504,82 @@ public function getFunctions(): array | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return $name; | ||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||
new TwigFunction('parse_value', function (array $property, string $mapAccess, string $v) { | ||||||||||||||||||||||||||||||||||
$required = $property['required'] ?? false; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Handle sub_schema | ||||||||||||||||||||||||||||||||||
if (isset($property['sub_schema']) && !empty($property['sub_schema'])) { | ||||||||||||||||||||||||||||||||||
$subSchema = \ucfirst($property['sub_schema']); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if ($property['type'] === 'array') { | ||||||||||||||||||||||||||||||||||
$arraySource = $required | ||||||||||||||||||||||||||||||||||
? "((IEnumerable<object>){$mapAccess})" | ||||||||||||||||||||||||||||||||||
: "({$v} as IEnumerable<object>)?"; | ||||||||||||||||||||||||||||||||||
return "{$arraySource}.Select(it => {$subSchema}.From(map: (Dictionary<string, object>)it)).ToList()"; | ||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
if ($required) { | ||||||||||||||||||||||||||||||||||
return "{$subSchema}.From(map: (Dictionary<string, object>){$mapAccess})"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return "({$v} as Dictionary<string, object>) is { } obj ? {$subSchema}.From(map: obj) : null"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Handle enum | ||||||||||||||||||||||||||||||||||
if (isset($property['enum']) && !empty($property['enum'])) { | ||||||||||||||||||||||||||||||||||
$enumName = $property['enumName'] ?? $property['name']; | ||||||||||||||||||||||||||||||||||
$enumClass = \ucfirst($enumName); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if ($required) { | ||||||||||||||||||||||||||||||||||
return "new {$enumClass}({$mapAccess}.ToString())"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return "{$v} == null ? null : new {$enumClass}({$v}.ToString())"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Fellmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Handle arrays | ||||||||||||||||||||||||||||||||||
if ($property['type'] === 'array') { | ||||||||||||||||||||||||||||||||||
$itemsType = $property['items']['type'] ?? 'object'; | ||||||||||||||||||||||||||||||||||
$src = $required ? $mapAccess : $v; | ||||||||||||||||||||||||||||||||||
$arraySource = $required | ||||||||||||||||||||||||||||||||||
? "((IEnumerable<object>){$src})" | ||||||||||||||||||||||||||||||||||
: "({$src} as IEnumerable<object>)?"; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
$selectExpression = match($itemsType) { | ||||||||||||||||||||||||||||||||||
'string' => 'x.ToString()', | ||||||||||||||||||||||||||||||||||
'integer' => 'Convert.ToInt64(x)', | ||||||||||||||||||||||||||||||||||
'number' => 'Convert.ToDouble(x)', | ||||||||||||||||||||||||||||||||||
'boolean' => '(bool)x', | ||||||||||||||||||||||||||||||||||
default => 'x' | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return "{$arraySource}.Select(x => {$selectExpression}).ToList()"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Handle integer/number | ||||||||||||||||||||||||||||||||||
if ($property['type'] === 'integer' || $property['type'] === 'number') { | ||||||||||||||||||||||||||||||||||
$convertMethod = $property['type'] === 'integer' ? 'Int64' : 'Double'; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if ($required) { | ||||||||||||||||||||||||||||||||||
return "Convert.To{$convertMethod}({$mapAccess})"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return "{$v} == null ? null : Convert.To{$convertMethod}({$v})"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Handle boolean | ||||||||||||||||||||||||||||||||||
if ($property['type'] === 'boolean') { | ||||||||||||||||||||||||||||||||||
$typeName = $this->getTypeName($property); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if ($required) { | ||||||||||||||||||||||||||||||||||
return "({$typeName}){$mapAccess}"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return "({$typeName}?){$v}"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+567
to
+575
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boolean cast can throw InvalidCastException. Lines 572 and 574 perform direct casts to Use safe conversion: if ($property['type'] === 'boolean') {
- $typeName = $this->getTypeName($property);
if ($required) {
- return "({$typeName}){$mapAccess}";
+ return "Convert.ToBoolean({$mapAccess})";
}
- return "({$typeName}?){$v}";
+ return "{$v} == null ? null : Convert.ToBoolean({$v})";
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Handle string type | ||||||||||||||||||||||||||||||||||
if ($required) { | ||||||||||||||||||||||||||||||||||
return "{$mapAccess}.ToString()"; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return "{$v}?.ToString()"; | ||||||||||||||||||||||||||||||||||
}, ['is_safe' => ['html']]), | ||||||||||||||||||||||||||||||||||
Fellmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ namespace {{ spec.title | caseUcfirst }} | |
ChunksUploaded = chunksUploaded; | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.