1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Text . Json ;
4
5
using commercetools . Sdk . Api . Models . Common ;
6
+ using commercetools . Sdk . Api . Serialization ;
5
7
6
8
namespace commercetools . Sdk . Api . Models . Types ;
7
9
@@ -59,9 +61,13 @@ public object GetValue(string fieldName)
59
61
public decimal ? AsDecimal ( string fieldName )
60
62
{
61
63
var value = GetValue ( fieldName ) ;
62
- if ( GetValue ( fieldName ) is decimal decimalValue ) { return decimalValue ; }
63
- if ( GetValue ( fieldName ) is long longValue ) { return Convert . ToDecimal ( longValue ) ; }
64
-
64
+ if ( value is decimal decimalValue ) { return decimalValue ; }
65
+ if ( value is long longValue ) { return Convert . ToDecimal ( longValue ) ; }
66
+ if ( value is JsonElement { ValueKind : JsonValueKind . Number } element )
67
+ {
68
+ return element . GetDecimal ( ) ;
69
+ }
70
+
65
71
return null ;
66
72
}
67
73
@@ -70,6 +76,10 @@ public object GetValue(string fieldName)
70
76
var value = GetValue ( fieldName ) ;
71
77
if ( value is decimal decimalValue ) { return Convert . ToInt64 ( decimalValue ) ; }
72
78
if ( value is long longValue ) { return longValue ; }
79
+ if ( value is JsonElement { ValueKind : JsonValueKind . Number } element )
80
+ {
81
+ return Convert . ToInt64 ( element . GetDecimal ( ) ) ;
82
+ }
73
83
74
84
return null ;
75
85
}
@@ -132,6 +142,10 @@ public object GetValue(string fieldName)
132
142
{
133
143
return longs . Select ( Convert . ToDecimal ) . ToList ( ) ;
134
144
}
145
+ if ( value is JsonElement { ValueKind : JsonValueKind . Array } elements && elements . GetFirstArrayElementValueKind ( ) == JsonValueKind . Number )
146
+ {
147
+ return elements . EnumerateArray ( ) . Select ( element => element . GetDecimal ( ) ) . ToList ( ) ;
148
+ }
135
149
136
150
return null ;
137
151
}
@@ -144,6 +158,10 @@ public object GetValue(string fieldName)
144
158
{
145
159
return decimals . Select ( Convert . ToInt64 ) . ToList ( ) ;
146
160
}
161
+ if ( value is JsonElement { ValueKind : JsonValueKind . Array } elements && elements . GetFirstArrayElementValueKind ( ) == JsonValueKind . Number )
162
+ {
163
+ return elements . EnumerateArray ( ) . Select ( element => Convert . ToInt64 ( element . GetDecimal ( ) ) ) . ToList ( ) ;
164
+ }
147
165
148
166
return null ;
149
167
}
0 commit comments