json.net

Convert Newtosoft JObject directly to BsonDocument

大城市里の小女人 提交于 2021-01-27 19:20:46
问题 Try to convert JObject to BsonDocument using this example https://www.newtonsoft.com/json/help/html/WriteJTokenToBson.htm ( BsonWriter obsolete, so I use BsonDataWriter ) var jObject = JObject.Parse("{\"name\":\"value\"}"); using var writer = new BsonDataWriter(new MemoryStream()); jObject.WriteTo(writer); var bsonData = writer.ToBsonDocument(); Console.WriteLine(bsonData.ToJson()); output: { "CloseOutput" : true, "AutoCompleteOnClose" : true, "Formatting" : 0, "DateFormatHandling" : 0,

JsonSerializer.CreateDefault().Populate(..) resets my values

百般思念 提交于 2021-01-27 17:37:21
问题 I have following class: public class MainClass { public static MainClass[] array = new MainClass[1] { new MainClass { subClass = new SubClass[2] { new SubClass { variable1 = "my value" }, new SubClass { variable1 = "my value" } } } }; public SubClass[] subClass; [DataContract] public class SubClass { public string variable1 = "default value"; [DataMember] // because only variable2 should be saved in json public string variable2 = "default value"; } } which I save as follows: File.WriteAllText

MVC4 JSON.Net oob

时间秒杀一切 提交于 2021-01-27 14:15:24
问题 I have read a few pieces of conflicting information regarding MVC 4's out-of-the-box support for JSON.NET I gathered that MVC is serializing JSON by default using JSON.NET now, however I still have the tell-tale MS date format in my JSON output. Is there any bootstrapping that still needs to be done? Example action: // // GET: /Test/ [HttpGet] public JsonResult Test() { return Json(new {date = DateTime.Now}, JsonRequestBehavior.AllowGet); } results in: { "date": "/Date(1355399663508)/" } 回答1:

JToken.WriteToAsync does not write to JsonWriter

拟墨画扇 提交于 2021-01-27 12:54:23
问题 I'm trying to create a middleware that changes the request in a certain way. I am able to read it and change the content but I cannot figure out how to correctly setup the stream writers to create a new body. When I call normalized.WriteToAsync(jsonWriter) the MemoryStream remains empty and consequently I receive the A non-empty request body is required. exception. What am I missing here? This is what I have so far: public async Task Invoke(HttpContext context) { if (context.Request

Difference between JToken.ToObject<T>() vs JToken.Value<T>()

别来无恙 提交于 2021-01-27 06:37:50
问题 What is the difference between the JToken.ToObject<T>() method and the JToken.Value<T>() extension method (the one without the key parameter)? var jToken = JToken.Parse("123"); var toObjectStrResult = jToken.ToObject<string>(); var valueStrResult = jToken.Value<string>(); // toObjectStrResult == valueStrResult == "123" var toObjectLongResult = jToken.ToObject<long>(); var valueLongResult = jToken.Value<long>(); // toObjectLongResult == valueLongResult == 123L 回答1: The difference is as follows

How to remove the square brackets in child rows of a json from SQL Server 2016?

☆樱花仙子☆ 提交于 2021-01-27 04:42:31
问题 I have a problem with the ouput for a query like this: SELECT Users.Id, Users.FbId, Users.Email, Users.Nombre, (SELECT * FROM AccessLevel WHERE AccessLevel.Id = Users.NivelAcceso FOR JSON PATH) AS NivelAcceso, (SELECT * FROM UserStatus WHERE UserStatus.Id = Users.Estatus FOR JSON PATH) AS Estatus FROM Users WHERE Users.Id = 1 FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER; The resulting of that is like this: { "Id": 1, "Email": "some@email.com", "NivelAcceso": [ { "Id": 1, "Clave": "Usuario" } ],

how to dynamic jsonignore according to user authorize?

女生的网名这么多〃 提交于 2021-01-27 04:21:10
问题 I use Metadata and JsonIgnore to remove special field from being serializing. [Authorize(Roles = "admin")] public class UserController : ApiController { public IEnumerable<user> Get() { using (var mydb = new ModelContainer()) { return mydb.userSet.ToList(); } } } [MetadataType(typeof(user_Metadata))] public partial class user { private class user_Metadata { [JsonIgnore] public virtual password { get; set; } public virtual adminFile { get; set; } } } How can I dynamic control which field

Deserializing with Json.Net, deserialize sub-object into string/similar holding the json?

大憨熊 提交于 2021-01-27 04:14:12
问题 I am trying to create a configuration file using Json that will hold configuration for various types of objects. Consider this file: { "cameras": [ { "type": "Some.Namespace.CameraClass", "assembly": "Some.Assembly", "configuration": { "ip": "127.0.0.1", "port": 8080 } } ] } At runtime I will use the two "type" and "assembly" properties to construct an object supporting a specific interface, and then I would like to load the configuration into that object. However, at compile time I do not

Deserializing with Json.Net, deserialize sub-object into string/similar holding the json?

点点圈 提交于 2021-01-27 04:14:10
问题 I am trying to create a configuration file using Json that will hold configuration for various types of objects. Consider this file: { "cameras": [ { "type": "Some.Namespace.CameraClass", "assembly": "Some.Assembly", "configuration": { "ip": "127.0.0.1", "port": 8080 } } ] } At runtime I will use the two "type" and "assembly" properties to construct an object supporting a specific interface, and then I would like to load the configuration into that object. However, at compile time I do not

Update JSON object using path and value

喜欢而已 提交于 2021-01-24 07:34:25
问题 I have a csv file that contains paths and values in the following format: path;value prop1.prop2.1;hello prop1.prop2.2;world prop1.prop2.3;! prop1.prop3.test;hi prop1.prop4;value And I wand to get it as json: { "prop1": { "prop2": { "1": "hello", "2": "world", "3": "!" } "prop3": { "test": "hi" } "prop4": "value" } } I've parsed csv file like this: Dictionary<string, string> dict = new Dictionary<string, string>(); while (csv.Read()) { string path = csv.GetField<string>(0); string value = csv