英文:
Prevent values transformation during deserialization/serialization
问题
我的应用程序对相当大的JSON文件进行了一些更改(不是序列化的.NET,只是配置文件)。重要的一点是,其他JSON部分应保持不变。我面临的问题是:在序列化/反序列化过程中,一些属性值以不希望的方式转换。示例:
1.333147768201288e+16 -> 13331477682012880.0
1684715304.486056 -> 1684715304.4860561
0.0 -> 0
其中一些转换发生在反序列化阶段,一些发生在序列化阶段。有些我可以处理,但有些变得非常棘手。
是否有一种方法可以告诉JSON模块以任何方式都不修改所有的值,而不进行最小的四舍五入/截断等操作?是否有一种优雅的方法可以做到这一点,而不需要临时解决方案和痛苦?谢谢!
PowerShell示例:
Add-Type -LiteralPath 'C:\Users\user\Documents\WindowsPowerShell\Modules\newtonsoft.json.0.2.201\libs\Newtonsoft.Json.dll';
$json = '{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}';
$deserialized = [Newtonsoft.Json.JsonConvert]::DeserializeObject($json);
$serialized = [Newtonsoft.Json.JsonConvert]::SerializeObject($deserialized);
# 将'c'转换为1684715304.4860561
write-host ($deserialized | select *)
# 将'a'转换为13331477682012880.0
write-host ($serialized)
英文:
My application does some changes in pretty big JSON files (not serialized .NET, just configs). The important moment - other JSON parts should stay untouched. The problem I faced is: during serialization/deserialization some of the property values transform in unwanted ways. Examples:
1.333147768201288e+16 -> 13331477682012880.0
1684715304.486056 -> 1684715304.4860561
0.0 -> 0
Some of these transformations take place in the deserialization stage and some - in the serialization stage. Some of them I can handle, but some becoming a nightmare.
Is there a way somehow tell the JSON module to leave all the values as-is, without a tiniest rounding/cutting/etc? is there an elegant way to do it without crutches and pain? Thanks!
PowerShell example:
Add-Type -LiteralPath 'C:\Users\user\Documents\WindowsPowerShell\Modules\newtonsoft.json.0.2.201\libs\Newtonsoft.Json.dll'
$json = '{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}'
$deserialized = [Newtonsoft.Json.JsonConvert]::DeserializeObject( $json )
$serialized = [Newtonsoft.Json.JsonConvert]::SerializeObject( $deserialized )
# transforms 'c' to 1684715304.4860561
write-host ( $deserialized | select * )
# transforms 'a' to 13331477682012880.0
write-host ( $serialized )
答案1
得分: 2
使用 PowerShell:
名称 值
---- -----
PS版本 7.3.4
PSEdition Core
Git提交ID 7.3.4
操作系统 Microsoft Windows 10.0.19045
平台 Win32NT
PS兼容版本 {1.0, 2.0, 3.0, 4.0…}
PS远程协议版本 2.3
序列化版本 1.1.0.1
WSMan堆栈版本 3.0
你是否尝试过类似以下内容:
使用命名空间 System.Text.Json
Write-Host "数据:"
$data = '{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}'
Write-Host $data
Write-Host "JSON字符串转对象:"
$obj = [JsonSerializer]::Deserialize( $data, [Object] )
Write-Host $obj
Write-Host ""
Write-Host "对象转JSON字符串:"
$json_str = ConvertTo-Json $obj
$json_str = [JsonSerializer]::Serialize( $obj, [Object] )
Write-Host $json_str
输出:
数据:
{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}
JSON字符串转对象:
{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}
对象转JSON字符串
{"a":1.333147768201288e+16,"c":1684715304.486056}
似乎保留了值/格式。
英文:
Using PowerShell:
Name Value
---- -----
PSVersion 7.3.4
PSEdition Core
GitCommitId 7.3.4
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Have you tried something like the following:
using namespace System.Text.Json
Write-Host "Data:"
$data = '{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}'
Write-Host $data
Write-Host "JSON String to Object:"
$obj = [JsonSerializer]::Deserialize( $data, [Object] )
Write-Host $obj
Write-Host ""
Write-Host "Object to JSON String"
$json_str = ConvertTo-Json $obj
$json_str = [JsonSerializer]::Serialize( $obj, [Object] )
Write-Host $json_str
Output:
Data:
{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}
JSON String to Object:
{
"a": 1.333147768201288e+16,
"c": 1684715304.486056
}
Object to JSON String
{"a":1.333147768201288e+16,"c":1684715304.486056}
Seems to preserve values / formatting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论