2

I need help when trying to convert a yaml to json it is passing all the properties to string, and it does not recognize the bool or integer fields.

I am using the YamlDotNet library, in c #. If anyone can help me I would appreciate it

// convertir string to stream
byte[] ArchivoConvertidobyteArray = Encoding.ASCII.GetBytes(archivoRuta);

var vigenciaYaml = (new StreamReader(new MemoryStream(ArchivoConvertidobyteArray)));
vigenciaJson = ConvertirYamlAJson(vigenciaYaml);

public static string ConvertirYamlAJson(TextReader yml)
{
    var deserializer = new DeserializerBuilder().Build(); 
    var yamlObject = deserializer.Deserialize(yml); 
    var serializer = new SerializerBuilder().JsonCompatible().Build(); 
    string json = serializer.Serialize(yamlObject); 
    return json;
}

Yaml file

# Definición del contrato
CodigoTipoContrato: BAS2
Descripcion: BASICO DOS
Empresa: DECORÉ
# Definición de conceptos del contrato
TiposConcepto:
- CodigoTipoConcepto: VPAG
  Descripcion: VALOR A PAGAR
  NaturalezaContable: debito
  PerfilContabilizacion: PCGRAL
  GrupoImpuestosArticulo: ''
  CodigoMoneda: COP
  PermiteCruce: true

What is expected

{
  "CodigoTipoContrato": "BAS2",
  "Descripcion": "BASICO DOS",
  "Empresa": "DECORÉ",
  "TiposConcepto": [
    {
      "CodigoTipoConcepto": "VPAG",
      "Descripcion": "VALOR A PAGAR",
      "NaturalezaContable": "debito",
      "PerfilContabilizacion": "PCGRAL",
      "GrupoImpuestosArticulo": "",
      "CodigoMoneda": "COP",
      "PermiteCruce": true
    }
  ]
}

This is how it is turning

{
  "CodigoTipoContrato": "BAS2",
  "Descripcion": "BASICO DOS",
  "Empresa": "DECORÉ",
  "TiposConcepto": [
    {
      "CodigoTipoConcepto": "VPAG",
      "Descripcion": "VALOR A PAGAR",
      "NaturalezaContable": "debito",
      "PerfilContabilizacion": "PCGRAL",
      "GrupoImpuestosArticulo": "",
      "CodigoMoneda": "COP",
      "PermiteCruce": "true"
    }
  ]
}
Jorge
  • 43
  • 6
  • 1
    i suggest you to write a sample in text and not with picture, helpers dont waste time to recreate sample for you. show your code will be a must, class, code c#... – Frenchy Nov 24 '21 at 13:05
  • Good morning, Frenchy I can not share the code since the error it shows me is by console and it is much easier to send the capture. – Jorge Nov 24 '21 at 13:32
  • what is your starting file yaml...?in your sample json is a string, so to parse your json you need to use a parser newtonjson for example, and you have to declare a class to parse your json string – Frenchy Nov 24 '21 at 13:40
  • The question was modified. – Jorge Nov 24 '21 at 13:40
  • Yes, but if I add an entity it converts it well, but if I remove it it converts everything to string – Jorge Nov 24 '21 at 13:49
  • It asks me for the entity Person (example) var p = deserializer.Deserialize(yml); I use it this way var p = deserializer.Deserialize(yml); – Jorge Nov 24 '21 at 13:51

2 Answers2

0

without entity or class, you have to force the type of value in yaml:

# Definición del contrato
CodigoTipoContrato: BAS2
Descripcion: BASICO DOS
Empresa: DECORÉ
# Definición de conceptos del contrato
TiposConcepto:
- CodigoTipoConcepto: VPAG
  Descripcion: VALOR A PAGAR
  NaturalezaContable: debito
  PerfilContabilizacion: PCGRAL
  GrupoImpuestosArticulo: ''
  CodigoMoneda: COP
  PermiteCruce: !!bool true

json result:

{
    "CodigoTipoContrato": "BAS2",
    "Descripcion": "BASICO DOS",
    "Empresa": "DECOR?",
    "TiposConcepto": [
        {
            "CodigoTipoConcepto": "VPAG",
            "Descripcion": "VALOR A PAGAR",
            "NaturalezaContable": "debito",
            "PerfilContabilizacion": "PCGRAL",
            "GrupoImpuestosArticulo": "",
            "CodigoMoneda": "COP",
            "PermiteCruce": true
        }
    ]
}

you have the same trick with int: !!int

Frenchy
  • 16,386
  • 3
  • 16
  • 39
0

Use this code to recognize all value types

 // convert string/file to YAML object to JSON
            var r = new StringReader(@"
                scalar: a scalar,
                minimum : 123,
                required : true
                sequence:
                  - one
                  - two
                ");
            var deserializer = new DeserializerBuilder()
                .WithAttemptingUnquotedStringTypeDeserialization()
                .Build();
            var yamlObject = deserializer.Deserialize(r);

            var serializer = new SerializerBuilder()
                .JsonCompatible()
                .Build();

            var json = serializer.Serialize(yamlObject);
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '23 at 15:12