public static bool AsBool(this IntermediateFieldValue value) { if (value.Data is bool b) { return(b); } else if (value.Data is int n) { return(n != 0); } else if (value.Data is long l) { return(l != 0); } else if (value.Data is string s) { if (s.Equals("yes", StringComparison.OrdinalIgnoreCase) || s.Equals("true", StringComparison.OrdinalIgnoreCase)) { return(true); } else if (s.Equals("no", StringComparison.OrdinalIgnoreCase) || s.Equals("false", StringComparison.OrdinalIgnoreCase)) { return(false); } } return((bool)value.Data); }
public static int AsNumber(this IntermediateFieldValue value) { if (value.Data is int n) { return(n); } else if (value.Data is long l) { return((int)l); } else if (value.Data is bool b) { return(b ? 1 : 0); } else if (value.Data is string s) { try { return(Convert.ToInt32(s)); } catch (FormatException) { throw new WixException(ErrorMessages.UnableToConvertFieldToNumber(s)); } } return((int)value.Data); }
public static string AsString(this IntermediateFieldValue value) { if (value?.Data == null) { return(null); } else if (value.Data is string s) { return(s); } else if (value.Data is int n) { return(n.ToString()); } else if (value.Data is long l) { return(l.ToString()); } else if (value.Data is bool b) { return(b ? "true" : "false"); } else if (value.Data is IntermediateFieldPathValue p) { return(p.Path); } return((string)value.Data); }
public static long?AsNullableLargeNumber(this IntermediateFieldValue value) { if (value?.Data == null) { return(null); } return(value.AsLargeNumber()); }
public static bool?AsNullableBool(this IntermediateFieldValue value) { if (value?.Data == null) { return(null); } return(value.AsBool()); }
internal static IntermediateField Deserialize(IntermediateFieldDefinition definition, Uri baseUri, JsonObject jsonObject) { var field = new IntermediateField(definition); if (jsonObject != null) { field.Value = IntermediateFieldValue.Deserialize(jsonObject, baseUri, definition.Type); } return(field); }
internal static IntermediateFieldValue Deserialize(JsonObject jsonObject, Uri baseUri, IntermediateFieldType type) { var context = jsonObject.GetValueOrDefault <string>("context"); if (!jsonObject.TryGetValue("data", out var data)) { throw new ArgumentException(); } var value = data; if (data is string) { } else if (data is long) { if (type == IntermediateFieldType.Number) { value = Convert.ToInt32(data); } } else if (data is JsonObject jsonData) { jsonData.TryGetValue("embeddedIndex", out var embeddedIndex); value = new IntermediateFieldPathValue { BaseUri = (embeddedIndex == null) ? null : baseUri, EmbeddedFileIndex = (embeddedIndex == null) ? null : (int?)Convert.ToInt32(embeddedIndex), Path = jsonData.GetValueOrDefault <string>("path"), }; } var previousValueJson = jsonObject.GetValueOrDefault <JsonObject>("prev"); var previousValue = (previousValueJson == null) ? null : IntermediateFieldValue.Deserialize(previousValueJson, baseUri, type); return(new IntermediateFieldValue { Context = context, Data = value, PreviousValue = previousValue }); }
public static IntermediateFieldPathValue AsPath(this IntermediateFieldValue value) { return((IntermediateFieldPathValue)value?.Data); }
internal static IntermediateFieldValue Deserialize(JsonObject jsonObject, Uri baseUri, IntermediateFieldType type) { var context = jsonObject.GetValueOrDefault <string>("context"); if (!jsonObject.TryGetValue("data", out var data)) { throw new ArgumentException(); } var value = data; switch (value) { case int intData: switch (type) { case IntermediateFieldType.Bool: value = intData != 0; break; case IntermediateFieldType.LargeNumber: value = Convert.ToInt64(data); break; case IntermediateFieldType.Path: case IntermediateFieldType.String: value = intData.ToString(); break; } break; case long longData: switch (type) { case IntermediateFieldType.Bool: value = longData != 0; break; case IntermediateFieldType.Number: value = Convert.ToInt32(longData); break; case IntermediateFieldType.Path: case IntermediateFieldType.String: value = longData.ToString(); break; } break; case JsonObject jsonData: jsonData.TryGetValue("embed", out var embed); value = new IntermediateFieldPathValue { BaseUri = (embed != null) ? baseUri : null, Embed = embed != null, Path = jsonData.GetValueOrDefault <string>("path"), }; break; // Nothing to do for this case, so leave it out. // case string stringData: // break; } var previousValueJson = jsonObject.GetValueOrDefault <JsonObject>("prev"); var previousValue = (previousValueJson == null) ? null : IntermediateFieldValue.Deserialize(previousValueJson, baseUri, type); return(new IntermediateFieldValue { Context = context, Data = value, PreviousValue = previousValue }); }