Формат один:
Код: Выделить всё
name: Tomato Chutney Three Ways
description: Tomato Chutney in three ways: Spicy Tomato Garlic Chutney, Sweet Tomato Chutney, and Oil-Free No-Cook Tomato Chutney.
cooking_instruction: |
For the Spicy Garlic Tomato Chutney:
- Heat 2 tbsp of oil in a pan.
- Serve with idli, dosas, and parathas.
For the Sweet Tomato Chutney:
- Heat 2 tbsp of oil, add asafoetida, fennel seeds, nigella seeds, dried red chilies, tomatoes, and salt. Cook for 5-7 minutes.
For the Oil-Free & No-Cook Tomato Chutney:
- Soak dried red chillies in white vinegar for 30 minutes.
Код: Выделить всё
name: Chicken Tikka Caesar Salad
description: Chicken Tikka Caesar Salad with romaine lettuce, bread croutons, and a flavorful dressing.
cooking_instruction:
- Take a bowl and combine hung curd, red chili powder, turmeric powder, coriander powder, garam masala, salt, ginger garlic paste, and chicken. Marinate for 15-20 minutes.
- Fry the marinated chicken in a grill pan with oil until crisp and golden brown.
Код: Выделить всё
name: Crispy Pata Pork Recipe (Deep Fried Pork Hock)
description: An easy deep fried pork recipe from the Philippines that is sure to win hearts over. What other pork recipes do you want to see?
cooking_instruction: |
- Boil everything except for the hock and the baking soda.
- Bring the liquid to a simmer, add in the hock with the lid on.
Код: Выделить всё
public class RecipeDto
{
public string Name { get; set; }
public string Description { get; set; }
public string CookingInstruction { get; set; }
}
Код: Выделить всё
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
var recipeDto = deserializer.Deserialize(yamlData);
Код: Выделить всё
No node deserializer was able to deserialize the node into type System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
Правильно ли это обрабатывать десериализацию поля, которое может быть скаляром или последовательностью? Есть ли лучший подход или какие-либо улучшения этого решения?
Изменить:
На данный момент я могу решить с помощью этого кода большую часть формат:
Код: Выделить всё
public class StringOrArrayConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(string);
}
public object ReadYaml(IParser parser, Type type)
{
var result = string.Empty;
if (parser.TryConsume(out var scalar))
{
result = scalar.Value;
}
else if (parser.TryConsume(out _))
{
var lines = new List();
while (parser.Current is not SequenceEnd)
{
if (parser.TryConsume(out var line))
{
lines.Add(line.Value);
}
else
{
parser.MoveNext();
}
}
parser.MoveNext(); // Consume the SequenceEnd event
result = string.Join("\n", lines);
}
else
{
throw new InvalidOperationException("Unexpected YAML format");
}
return result;
}
public void WriteYaml(IEmitter emitter, object value, Type type)
{
var str = (string)value;
emitter.Emit(new Scalar(str));
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... -scalar-or
Мобильная версия