Hi everone,
For a client i'm updating a Exact Online API. In this API they use the old ASP framework. Now i get a lot of errors in my code and one of them is a error at JavaScriptSerializer. This isn't supported in ASP.NET core anymore. Now i read on the internet that the best replacement for this is Newtonsoft.Json.
This is the old code:
public static string GetJsonObject(string response)
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new JssDateTimeConverter() });
var oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string output;
try
{
var dict = (Dictionary<string, object>)serializer.Deserialize<object>(response);
var d = (Dictionary<string, object>)dict["d"];
output = GetJsonFromDictionary(d);
}
finally
{
Thread.CurrentThread.CurrentCulture = oldCulture;
}
return output;
}This is what i'm trying to do with Newtonsoft.Json:
public static string GetJsonObject(string response)
{
var serializer = new JsonSerializer();
serializer.Converters.Add(new JsonConverter { new JssDateTimeConverter() });
var oldCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
//var serializer = new JavaScriptSerializer();
//serializer.RegisterConverters(new JssDateTimeConverter});
string output;
try
{
var converter = new ExpandoObjectConverter();
var dict = (Dictionary<string, object>)serializer.Deserialize<object>(response); <- HERE IS THE ERROR
var d = (Dictionary<string, object>)dict["d"];
output = GetJsonFromDictionary(d);
}
finally
{
CultureInfo.CurrentCulture = oldCulture;
}
return output;
}The only problem is that i get the following error in the try catch:
Cannot convert from 'string' to 'Newtonsoft.Json.JsonReader'
Does someone know how to convert the code to Newtonsoft.Json? For the people that want to know what's in the JssDateTimeConverter class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Formatters.Json;
using Microsoft.AspNetCore.JsonPatch;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Site.Models.Exact.Helpers
{
/// <summary>
/// Custom JavaScriptConverter for parsing datetime value correctly
/// </summary>
public class JssDateTimeConverter : StringEnumConverter // : JavaScriptConverter
{
public IEnumerable<Type> SupportedTypes //override
{
get
{
return new[] { typeof(Object) };
}
}
public object Deserialize(IDictionary<string, object> dictionary, Type type, JsonSerializer serializer)
{
var keys = new List<string>(dictionary.Keys);
foreach (string key in keys)
{
object entity = dictionary[key];
// Check if content is a dictionary > send to this method recursively
if (entity != null && entity.GetType() == typeof(Dictionary<string, object>))
{
var value = (Dictionary<string, object>)entity;
Deserialize(value, type, serializer);
}
else
{
var value = entity;
if (value != null)
{
// Set EPOCH datetime
Type valueType = value.GetType();
if (valueType == typeof(DateTime))
{
var date = (DateTime)entity;
TimeSpan t = (date - new DateTime(1970, 1, 1));
double timestamp = t.TotalMilliseconds;
dictionary[key] = string.Format("/Date({0})/", timestamp);
}
// For collection within this collection > send to this method recursively
if (valueType == typeof(ArrayList))
{
IEnumerable<object> dictionaries = ((ArrayList)value).ToArray().Where(x => x.GetType() == typeof(Dictionary<string, object>));
foreach (var dict in dictionaries)
{
Deserialize((Dictionary<string, object>)dict, type, serializer);
}
}
}
}
}
return dictionary;
}
public IDictionary<string, object> Serialize(object obj, JsonSerializer serializer)
{
return null;
}
}
}
thanks in advance!