@@ -59,7 +59,7 @@ public ValueContainer(string stringValue)
5959
6060 public ValueContainer ( float floatValue )
6161 {
62- _value = floatValue ;
62+ _value = Convert . ToDouble ( floatValue ) ;
6363 _type = ValueType . Float ;
6464 }
6565
@@ -114,8 +114,9 @@ public ValueContainer()
114114
115115 public ValueContainer ( JToken json )
116116 {
117- _type = ValueType . Object ;
118- _value = JsonToValueContainer ( json ) . GetValue < Dictionary < string , ValueContainer > > ( ) ;
117+ var v = JsonToValueContainer ( json ) ;
118+ _type = v . _type ;
119+ _value = v . _value ;
119120 }
120121
121122 public ValueType Type ( )
@@ -223,32 +224,51 @@ public Dictionary<string, ValueContainer> AsDict()
223224
224225 private ValueContainer JsonToValueContainer ( JToken json )
225226 {
226- if ( json is JObject jObject )
227+ switch ( json )
227228 {
228- var dictionary = json . ToDictionary ( pair => ( ( JProperty ) pair ) . Name , token =>
229+ case JObject jObject :
229230 {
230- if ( token . Children ( ) . Count ( ) != 1 ) return JsonToValueContainer ( token . Children ( ) . First ( ) ) ;
231+ var dictionary = json . ToDictionary ( pair => ( ( JProperty ) pair ) . Name , token =>
232+ {
233+ if ( token . Children ( ) . Count ( ) != 1 ) return JsonToValueContainer ( token . Children ( ) . First ( ) ) ;
234+
235+ var t = token . First ;
236+ return t . Type switch
237+ {
238+ JTokenType . String => new ValueContainer ( t . Value < string > ( ) , true ) ,
239+ JTokenType . Boolean => new ValueContainer ( t . Value < bool > ( ) ) ,
240+ JTokenType . Integer => new ValueContainer ( t . Value < int > ( ) ) ,
241+ JTokenType . Float => new ValueContainer ( t . Value < float > ( ) ) ,
242+ _ => JsonToValueContainer ( token . Children ( ) . First ( ) )
243+ } ;
244+ } ) ;
245+
246+ return new ValueContainer ( dictionary ) ;
247+ }
248+ case JArray jArray :
249+ return jArray . Count > 0 ? new ValueContainer ( ) : JArrayToValueContainer ( jArray ) ;
250+ case JValue jValue :
251+ if ( jValue . HasValues )
252+ {
253+ throw new PowerAutomateMockUpException (
254+ "When parsing JToken to ValueContainer, the JToken as JValue can only contain one value." ) ;
255+ }
231256
232- var t = token . First ;
233- return t . Type switch
257+ return jValue . Type switch
234258 {
235- JTokenType . String => new ValueContainer ( t . Value < string > ( ) , true ) ,
236- JTokenType . Boolean => new ValueContainer ( t . Value < bool > ( ) ) ,
237- JTokenType . Integer => new ValueContainer ( t . Value < int > ( ) ) ,
238- JTokenType . Float => new ValueContainer ( t . Value < float > ( ) ) ,
239- _ => JsonToValueContainer ( token . Children ( ) . First ( ) )
259+ JTokenType . Boolean => new ValueContainer ( jValue . Value < bool > ( ) ) ,
260+ JTokenType . Integer => new ValueContainer ( jValue . Value < int > ( ) ) ,
261+ JTokenType . Float => new ValueContainer ( jValue . Value < float > ( ) ) ,
262+ JTokenType . Null => new ValueContainer ( ) ,
263+ JTokenType . String => new ValueContainer ( jValue . Value < string > ( ) ) ,
264+ JTokenType . None => new ValueContainer ( ) ,
265+ JTokenType . Guid => new ValueContainer ( jValue . Value < Guid > ( ) . ToString ( ) ) ,
266+ _ => throw new PowerAutomateMockUpException (
267+ $ "{ jValue . Type } is not yet supported in ValueContainer conversion")
240268 } ;
241- } ) ;
242-
243- return new ValueContainer ( dictionary ) ;
244- }
245-
246- if ( json is JArray jArray )
247- {
248- return jArray . Count > 0 ? new ValueContainer ( ) : JArrayToValueContainer ( jArray ) ;
269+ default :
270+ throw new PowerAutomateMockUpException ( "Could not parse JToken to ValueContainer." ) ;
249271 }
250-
251- throw new Exception ( ) ;
252272 }
253273
254274 private ValueContainer JArrayToValueContainer ( JArray json )
@@ -373,6 +393,19 @@ public bool Equals(ValueContainer other)
373393
374394 return thisDict . Count == otherDict . Count && ! thisDict . Except ( otherDict ) . Any ( ) ;
375395 }
396+ case ValueType . Integer when other . _type == ValueType . Float :
397+ var v = ( double ) _value ;
398+ return Math . Abs ( Math . Floor ( v ) - other . _value ) < double . Epsilon ;
399+ case ValueType . Float when other . _type == ValueType . Integer :
400+ {
401+ return Math . Abs ( Math . Floor ( _value ) - other . _value ) < double . Epsilon ;
402+ }
403+ case ValueType . Float :
404+ {
405+ // TODO: Figure out how to handle comparison and in general how to handle float/double..
406+ // assignee: thygesteffensen
407+ return Math . Abs ( _value - other . _value ) < 0.01 ;
408+ }
376409 default :
377410 return Equals ( _value , other . _value ) && _type == other . _type ;
378411 }
0 commit comments