/// <inheritdoc /> public override void OnValue(string value, ValueType type) { if (type == ValueType.Null) { JsonWriter.WriteNull(); return; } if (type == ValueType.Boolean && bool.TryParse(value, out var boolValue)) { JsonWriter.WriteValue(boolValue); } else { if (type == ValueType.Number && decimal.TryParse(value, out var numberValue)) { if (numberValue % 1 == 0) { JsonWriter.WriteValue((long)numberValue); } else { JsonWriter.WriteValue(numberValue); } } else { JsonWriter.WriteValue(value); } } }
/// <summary> /// Resolves literal value of <see cref="Alias"/>. /// </summary> /// <param name="alias">Instance of <see cref="Alias"/>.</param> /// <param name="valueType">Value type of the resolved literal.</param> /// <returns>String representing literal value of the <see cref="Alias"/>.</returns> protected string ResolveValueAlias(Alias alias, out ValueType valueType) { var aliasDef = alias.AliasDefinition; AliasContext.Push(new AliasContextInfo(alias, CurrentModuleMember)); CurrentModuleMember = aliasDef; string result; if (aliasDef.ValueType == ValueType.LiteralChoice) { result = ResolveChoiceValue(alias, out valueType); } else { result = aliasDef.PairValue == null ? ResolveValue(aliasDef, out valueType) : ResolvePairValue(aliasDef.PairValue, out valueType); } CurrentModuleMember = AliasContext.Pop().ModuleMember; return(result); }
private string ResolvePairValue(object pairValue, out ValueType valueType) { if (pairValue is Parameter value) { return(ResolveValueParameter(value, out valueType)); } var alias = pairValue as Alias; valueType = ValueType.None; return(alias != null?ResolveValueAlias(alias, out valueType) : null); }
protected string ResolveNodeValue(IMappedPair pair, out ValueType valueType) { if (pair.ValueType != ValueType.DoubleQuotedString && pair.ValueType != ValueType.Concatenation) { if (((Pair)pair).PairValue != null) { return(ResolvePairValue(((Pair)pair).PairValue, out valueType)); } valueType = pair.ValueType; return(((Pair)pair).Delimiter != DelimiterEnum.None ? ((Pair)pair).Value : ((Pair)pair).Name); } return(ResolveValueInInterpolation(pair, out valueType)); }
private ValueType GetJsonValueType(string value, ValueType defaultType) { if (value == "true" || value == "false") { return(ValueType.Boolean); } if (value == "null") { return(ValueType.Null); } if (Regex.Match(value, @"^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$").Success) { return(ValueType.Number); } return(defaultType); }
/// <summary> /// Resolves value of the value parameter. /// </summary> /// <param name="parameter">Parameter.</param> /// <param name="valueType">Calculated <see cref="ValueType"/></param> /// <returns>Literal value of parameter.</returns> protected string ResolveValueParameter(Parameter parameter, out ValueType valueType) { var aliasContext = GetAliasContextForParameter(parameter).Alias; if (parameter.Name == "_") { //Resolving default value parameter if (aliasContext.PairValue != null) { return(ResolvePairValue(aliasContext.PairValue, out valueType)); } return(ResolveValue(aliasContext, out valueType)); } var argument = aliasContext.Arguments.FirstOrDefault(a => a.Name == parameter.Name); if (argument != null) { if (argument.PairValue != null) { return(ResolvePairValue(argument.PairValue, out valueType)); } if (((IMappedPair)argument?.Parent).BlockType == BlockType.JsonObject && argument.Entities.Count > 0) { var ent = argument.Entities.First(e => e != null); valueType = ((IMappedPair)ent).ValueType; return(ent.Value ?? ent.Name); } valueType = ((IMappedPair)argument).ValueType; return(argument.Value); } //if argument is not found lookup default value in the Alias Definition //var paramDef = aliasContext.AliasDefinition.Parameters.First(p => p.Name == parameter.Name); //If parameters default value is Parameter or Alias then resolve it if (parameter.PairValue != null) { return(ResolvePairValue(parameter.PairValue, out valueType)); } valueType = parameter.ValueType; return(parameter.Value); }
/// <summary> /// Resolves literal value of the pair. /// </summary> /// <param name="pair"><see cref="Pair"/> with the literal value.</param> /// <param name="valueType">Calculated type of the value.</param> /// <returns>String representing literal value of the pair.</returns> protected string ResolveValue(IMappedPair pair, out ValueType valueType) { if (pair.ValueType != ValueType.DoubleQuotedString && pair.ValueType != ValueType.Concatenation) { if (((Pair)pair).PairValue != null) { return(ResolvePairValue(((Pair)pair).PairValue, out valueType)); } if (((Pair)pair).Assignment.IsObjectAssignment() && pair.IsValueNode) //JSONObject block type. Value is defined by the first child { return(ResolveValue((IMappedPair)((IContainer)pair).Entities.First(), out valueType)); } valueType = pair.ValueType; return(((Pair)pair).Assignment != AssignmentEnum.None ? ((Pair)pair).Value : ((Pair)pair).Name); } return(ResolveValueInInterpolation(pair, out valueType)); }
private void ResolveConcatenation(Pair pair) { var p = pair as IPairWithInterpolation; if (p?.InterpolationItems == null || p.InterpolationItems.Count == 0) { OnValue("", ValueType.Concatenation); return; } var sb = new StringBuilder(); ValueType resultValueType = ValueType.None; foreach (var item in p.InterpolationItems) { ValueType valueType = ValueType.None; object value = item as Parameter; if (value != null) { sb.Append(ResolveValueParameter((Parameter)value, out valueType)); } else { value = item as Alias; if (value != null) { sb.Append(ResolveValueAlias((Alias)value, out valueType)); } else if (((IMappedPair)item).IsValueNode) { sb.Append(ResolveValue((IMappedPair)item, out valueType)); } } resultValueType = resultValueType == ValueType.None ? valueType : ValueType.Concatenation; } if (resultValueType == ValueType.None) { resultValueType = ValueType.Concatenation; } OnValue(sb.ToString(), resultValueType); }
protected string ResolveValueParameter(Parameter parameter, out ValueType valueType) { var aliasContext = GetAliasContextForParameter(parameter); if (parameter.Name == "_") { //Resolving default value parameter if (aliasContext.Alias.PairValue != null) { return(ResolvePairValue(aliasContext.Alias.PairValue, out valueType)); } return(ResolveNodeValue(aliasContext.Alias, out valueType)); } var argument = aliasContext.Alias.Arguments.FirstOrDefault(a => a.Name == parameter.Name); if (argument != null) { if (argument.PairValue != null) { return(ResolvePairValue(argument.PairValue, out valueType)); } valueType = ((IMappedPair)argument).ValueType; return(argument.Value); } //if argument is not found lookup default value in the Alias Definition //var paramDef = aliasContext.AliasDefinition.Parameters.First(p => p.Name == parameter.Name); //If parameteres default value is Parameter or Alias then resolve it if (parameter.PairValue != null) { return(ResolvePairValue(parameter.PairValue, out valueType)); } valueType = parameter.ValueType; return(parameter.Value); }
protected string ResolveValueAlias(Alias alias, out ValueType valueType) { var aliasDef = NamespaceResolver.GetAliasDefinition(alias.Name); AliasContext.Push(new AliasContext { AliasDefinition = aliasDef, Alias = alias, AliasNsInfo = GetContextNsInfo() }); string result; if (aliasDef.ValueType == ValueType.LiteralChoice) { result = ResolveChoiceValue(alias, out valueType); } else { result = aliasDef.PairValue == null?ResolveNodeValue(aliasDef, out valueType) : ResolvePairValue(aliasDef.PairValue, out valueType); } AliasContext.Pop(); return(result); }
protected override string ResolveChoiceValue(Pair pair, out ValueType valueType) { var choice = _choiceStack.Peek(); var choiceInfo = FindChoiceInfo(choice, pair); if (choice.ChoiceNode != pair) { _choiceStack.Push(choiceInfo); } string result = string.Empty; valueType = ValueType.OpenString; if (choice.Children != null) { _choiceStack.Push(choiceInfo.Children[0]); result = ResolveNodeValue((IMappedPair)choiceInfo.Children[0].ChoiceNode, out valueType); _choiceStack.Pop(); } if (choice.ChoiceNode != pair) { _choiceStack.Pop(); } return(result); }
public override void OnValue(string value, ValueType type) { _xmlTextWriter.WriteString(value); }
protected override string ResolveChoiceValue(Pair pair, out ValueType valueType) // todo: Implement choice validation { valueType = ValueType.None; return(""); }
protected virtual string ResolveChoiceValue(Pair pair, out ValueType valueType) { throw new NotImplementedException(); }
/// <summary> /// Method is called for pair with literal choice assignment <b>=::</b>. /// </summary> /// <param name="pair">Pair with literal choice assignment</param> /// <param name="valueType">Literal value type of the resolved case.</param> /// <returns>String representing resolved literal value of the pair.</returns> protected abstract string ResolveChoiceValue(Pair pair, out ValueType valueType);
/// <summary> /// Method called when the value of the current pair is resolved. /// </summary> /// <param name="value">String representing the value of the current pair.</param> /// <param name="type">Type of value.</param> public virtual void OnValue(string value, ValueType type) { }
private string ResolveValueInInterpolation(IMappedPair node, out ValueType valueType) { var sb = new StringBuilder(); object previousLine = null; int eolCount = 0; valueType = node.ValueType; //default value if (((IPairWithInterpolation)node).InterpolationItems != null) { foreach (var item in ((IPairWithInterpolation)node).InterpolationItems) { if (item is EscapeMatch escapeMatch) { if (escapeMatch is EolEscapeMatch match) { ResolveDqsEol(match, sb, node.ValueIndent, previousLine); eolCount++; } else { if (eolCount == 1) { sb.Append(" "); } ResolveDqsEscape(escapeMatch, sb); eolCount = 0; } previousLine = item; continue; } if (eolCount == 1) { sb.Append(" "); } eolCount = 0; if (item is Alias alias) { sb.Append(ResolveValueAlias(alias, out valueType)); previousLine = item; continue; } if (item is Parameter param) { sb.Append(ResolveValueParameter(param, out valueType)); previousLine = item; continue; } if (item is Element element) { sb.Append(ResolveValue(element, out valueType)); previousLine = item; continue; } sb.Append(item); previousLine = item; } if (((IPairWithInterpolation)node).InterpolationItems.Count > 1) { valueType = node.ValueType; //restore default value if there are more then 1 interpolation items } } return(sb.ToString()); }