Last active
June 21, 2023 07:15
-
-
Save leomicheloni/fa6de5a4bfe793e7e89a57cd7a3a0790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
namespace ConsoleApp1 | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var message = new Message | |
{ | |
Role = "user", | |
Content = "What is the Microsoft mission" | |
}; | |
var message2 = new Message | |
{ | |
Role = "bot", | |
Content = "Empower every person and every organization on the planet to achieve more." | |
}; | |
var messageList = new MessageList | |
{ | |
Messages = new List<Message> { message, message2 } | |
}; | |
var json = JsonConvert.SerializeObject(messageList, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); | |
Console.WriteLine(json); | |
} | |
} | |
public class Message | |
{ | |
public string Role { get; set; } | |
public string Content { get; set; } | |
} | |
public class MessageList | |
{ | |
public List<Message> Messages { get; set; } | |
} | |
// "messages": [{"role": "user", "content": "What is the Microsoft mission"}] | |
} |
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml;
namespace ConsoleEliminame { public class MiClase { [JsonProperty("messages")] public Message[] Messages { get; set; } }
public class Message { [JsonProperty("role")] public string Role { get; set; } [JsonProperty("content")] public string Content { get; set; } } internal class Program { static void Main(string[] args) { var miClase = new MiClase { Messages = new Message[] { new Message { Role = "Message1", Content = "What is the Microsoft mission" }, new Message { Role = "Message2", Content = "What is the Google" }, new Message { Role = "Message3", Content = "What is the Linux" } } }; string json = JsonConvert.SerializeObject(miClase, Newtonsoft.Json.Formatting.Indented); Console.WriteLine(json); } }
}
Instead of using attributes on any property to get lowercase properties I use a ContractResolver, looks more clean for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleEliminame
{
public class MiClase
{
[JsonProperty("messages")]
public Message[] Messages { get; set; }
}
}