System.TypeLoadException using Newtonsoft JSON deserialize - json

I am truly stumped here. Here is my JSON returned:
{"ResponseData":[{"ClusterID":"c02f1f5c-c61b-4f2c-ab5a-249966b3cdef","ClusterName":"Northeast","Courses":[{"CourseID":"8ab4f2b3-8160-4d7e-b79f-8d8b58926cc0","CourseName":"Home Course","SubCourses":[{"SubCourseName":"SubCourse1","SubCourseNumber":18}]},{"CourseID":"b3223464-333b-4c54-89c2-23908e0510c9","CourseName":"Away Course","SubCourses":[{"SubCourseName":"SubCourse1","SubCourseNumber":19}]}],"IsHomeCluster":true},"ResponseErrors":[]}
This is my code to deserialize:
JArray jArr = (JArray)JsonConvert.DeserializeObject(json);
foreach (var item in jArr) {
foreach (var subitem in item["ResponseData"]) {
Console.WriteLine (subitem ["ClusterID"]);
}
}
Project compiles fine, but when I run it in the simulator, I get this error:
System.TypeLoadException: A type load exception has occurred. at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize
(Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean
checkAdditionalContent) [0x00000] in :0 at
Newtonsoft.Json.JsonSerializer.DeserializeInternal
(Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000]
in :0 at
Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader
reader, System.Type objectType) [0x00000] in :0 at
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value,
System.Type type, Newtonsoft.Json.JsonSerializerSettings settings)
[0x00000] in :0 at
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value)
[0x00000] in :0 at
AppMultiView.CourseInformationScreen.ViewDidLoad () [0x00029] in
/Users/Dan/Desktop/AppTouch_dev /Screens/CourseInformationScreen.cs:48
at (wrapper managed-to-native)
MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_bool
(intptr,intptr,intptr,bool) at
MonoTouch.UIKit.UINavigationController.PushViewController
(MonoTouch.UIKit.UIViewController viewController, Boolean animated)
[0x00021] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UINavigationController.g.cs:176
at AppMultiView.HomeScreen.m__2 (System.Object sender,
System.EventArgs e) [0x00016] in /Users/Dan/Desktop/AppTouch_dev
/Screens/HomeScreen.cs:75 at
MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 at
(wrapper managed-to-native)
MonoTouch.UIKit.UIApplication:UIApplicationMain
(int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main
(System.String[] args, System.String principalClassName, System.String
delegateClassName) [0x0004c] in
/Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at AppMultiView.Application.Main (System.String[] args) [0x00000] in
/Users/Dan/Desktop/AppTouch_dev /Main.cs:18
Am I trying to deserialize the wrong thing?
Any pointers will be greatly appreciated.

I'm not entirely sure what the issue you are seeing is. It may just be that your json is badly formatted.
It it helps, the way I'd normally tackle this is to:
Use a tool like http://chris.photobooks.com/json/ to validate the JSON - in this case, this revealed to me an error - your "ResponseData" array was not terminated. The fixed code is:
{"ResponseData":[
{"ClusterID":"c02f1f5c-c61b-4f2c-ab5a-249966b3cdef","ClusterName":"Northeast",
"Courses":
[
{"CourseID":"8ab4f2b3-8160-4d7e-b79f-8d8b58926cc0","CourseName":"Home Course","SubCourses":
[{"SubCourseName":"SubCourse1","SubCourseNumber":18}]},
{"CourseID":"b3223464-333b-4c54-89c2-23908e0510c9","CourseName":"Away Course","SubCourses":
[{"SubCourseName":"SubCourse1","SubCourseNumber":19}]}
],
"IsHomeCluster":true}
]
,"ResponseErrors":[]}
copy the corrected JSON into http://json2csharp.com/
This gives me classes like:
public class SubCours
{
public string SubCourseName { get; set; }
public int SubCourseNumber { get; set; }
}
public class Cours
{
public string CourseID { get; set; }
public string CourseName { get; set; }
public List<SubCours> SubCourses { get; set; }
}
public class ResponseData
{
public string ClusterID { get; set; }
public string ClusterName { get; set; }
public List<Cours> Courses { get; set; }
public bool IsHomeCluster { get; set; }
}
public class RootObject
{
public List<ResponseData> ResponseData { get; set; }
public List<object> ResponseErrors { get; set; }
}
Use the JsonConvert.DeserializeObject<RootObject>(json) to get a deserialized RootObject
There is also a new Paste As Classes feature for JSON available: http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx

Related

System.InvalidCastException error in EF Core (usdig Raw Sql) when query returns no rows (but working when query returns rows)

In a .Net 5 Web Api application, I am reading some rows from a MySql table (using the Pomelo library).
I am using a raw sql query. So:
MySqlParameter propertyIdParam = new MySqlParameter()
{
ParameterName = "#propertyId",
MySqlDbType = MySqlDbType.Int32,
Direction = System.Data.ParameterDirection.Input,
Value = 7
};
List<PropertyRepairsDto> propertyRepairs = await _context.PropertyRepairs
.FromSqlRaw("some sql query", propertyIdParam)
.AsNoTracking()
.ToListAsync();
The strange thing is that this works well, until I provide a parameter ("propertyId") which doesn't exist - so that the query returns no rows. In that case, I get the following exception:
System.InvalidCastException: Specified cast is not valid.
at MySqlConnector.Core.Row.GetInt32(Int32 ordinal) in /_/src/MySqlConnector/Core/Row.cs:line 211
at MySqlConnector.MySqlDataReader.GetInt32(Int32 ordinal) in /_/src/MySqlConnector/MySqlDataReader.cs:line 238
at lambda_method848(Closure , QueryContext , DbDataReader , Int32[] )
at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at PropWorx.API.Controllers.PropertiesController.GetPropertyRepairsAsync(Int32 id) in C:\Users\fabsr\source\repos\PropWorx.API\PropWorx.API\Controllers\PropertiesController.cs:line 194
at lambda_method479(Closure , Object )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at PropWorx.API.Middlewares.TenantIdentifier.Invoke(HttpContext httpContext, SharedContext sharedContext) in C:\Users\fabsr\source\repos\PropWorx.API\PropWorx.API\Middlewares\TenantIdentifier.cs:line 52
at PropWorx.API.Middlewares.WebSocketsMiddleware.Invoke(HttpContext httpContext) in C:\Users\fabsr\source\repos\PropWorx.API\PropWorx.API\Middlewares\WebSocketsMiddleware.cs:line 28
at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Connection: keep-alive
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, br
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1ERXpSRUUwUWpFMk9VTkdOa0kyUlRJMFJEUXdNakExUTBFeE1EUkNRalpDUmtWR1JVTXhOQSJ9.eyJpc3MiOiJodHRwczovL2NvZGV4Y3JlYXRpb25zLmV1LmF1dGgwLmNvbS8iLCJzdWIiOiJhdXRoMHw1OTI2YjEwYTAxZTkxZDE0MGU5OWVlNjQiLCJhdWQiOlsiaHR0cHM6Ly9wcm9wd29yeC5jby56YS9hcGkiLCJodHRwczovL2NvZGV4Y3JlYXRpb25zLmV1LmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE2MTAwMDkyMjQsImV4cCI6MTYxMjYwMTIyNCwiYXpwIjoiUnppNzVkU1dpcVlFYlJsVjU1N0lFd1dQYUI5Qmwzd3giLCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIn0.WK2gtsbWtiz1WIkJrO56Ap8fqyyyLBs1wiKcs0KvTVTPKlzVyicf6J9S-9PMDGs1uT-gho7fVW6gLVv9XzLCulcR4x3KFrNmLSW9lvsj9fyKPMQ4Udp4A2UdfYoogRXduB62QwqRhkYGbNkI-tSGgMwF-zgClirKalpY6oKs9yMY6qr_XC4ZzTI27Pd4W9wverLtLT0-1WNlh4ynUF_xE5uvfKfh0KhBITs-KASUBMz7WcloSehNqvnkwOOdLCcWtkcbcms4LRklwEzpQvXiuCwAgjpjq980fjFjYSISjD5paqBM_rdpvG4Yz4OqI6FXtccuszbc_1WcXe7O_CSuTg
Host: localhost:5001
Referer: http://localhost:5000/api/properties/6823/repairs
User-Agent: PostmanRuntime/7.26.8
Content-Length: 2856
clientId: 273
Postman-Token: fe2fcc49-3f1b-451b-8e06-b808fa28964c
I don't understand how it's possible that it works fine when the query returns rows (so it's mapping the database data correctly to the model fields), but when the query returns no rows (which I would assume should simply result in a List of zero items) it throws an exception.
The exception suggests a problem converting a value from the database to int32. At first, I thought it might be a null that's trying to be mapped to a non-nullable integer in my model, but ALL integer fields in my model are nullable. But even so, since the query is not returning any rows, surely it shouldnt even be a problem as there is nothing to map?
I know it's difficult to assist me without seeing the database structures, models, queries, etc. It's just that the query and tables are quite big, and I thought perhaps someone has experienced this problem before, where things work until a query is called which returns no rows, in which case a System.InvalidCastException is thrown.
Here is the model I am mapping to, for what it's worth:
public class PropertyRepairsDto
{
public int? Id { get; set; }
public int? FileId { get; set; }
public string FileNum { get; set; }
public bool? IsRequired { get; set; }
public string Info { get; set; }
public bool? QuotesObtained { get; set; }
public decimal? QuoteAmount { get; set; }
public string ContractorAppointed { get; set; }
public bool? IsPaid { get; set; }
public decimal? AmountPaid { get; set; }
public DateTime? JobDate { get; set; }
public bool? PaymentRequired { get; set; }
public DateTime? PaymentDate { get; set; }
public string PaymentReference { get; set; }
public DateTime? ReportedAt { get; set; }
public int? ContractorId { get; set; }
public string Contractor { get; set; }
public bool? IsFixedByOwner { get; set; }
public DateTime? FollowUpDate { get; set; }
public string WorkOrderNumber { get; set; }
public bool? IsCompleted { get; set; }
public DateTime? CompletedDate { get; set; }
public bool? IsInvoiceReceived { get; set; }
public DateTime? InvoiceReceivedAt { get; set; }
public decimal? InvoiceAmount { get; set; }
public bool? IsInvoiceApproved { get; set; }
public DateTime? InvoiceApprovedAt { get; set; }
public string InvoiceApprovedBy { get; set; }
public bool? ChargeToTenant { get; set; }
public bool? IsReportedToOwner { get; set; }
public DateTime? ReportedToOwnerAt { get; set; }
public int? InvoiceDocId { get; set; }
public int? ReportedById { get; set; }
public string ReportedBy { get; set; }
public int? PriorityId { get; set; }
public string Priority { get; set; }
public int? QuotesRequired { get; set; }
public bool? InvoiceOwner { get; set; }
public bool? InvoiceTenant { get; set; }
public bool? IsReportSentToTenant { get; set; }
public DateTime? ReportSentToTenantAt { get; set; }
public bool? IsJobCardSentToServiceProvider { get; set; }
public int? JobCardSentToId { get; set; }
public string JobCardSentTo { get; set; }
public int? JobCardSentToServiceProviderById { get; set; }
public string JobCardSentToServiceProviderBy { get; set; }
public DateTime? JobCardSentToServiceProviderAt { get; set; }
public int? StatusId { get; set; }
public string Status { get; set; }
public int? PropertyId { get; set; }
public int? AssignedToId { get; set; }
public string AssignedTo { get; set; }
public string Comments { get; set; }
public DateTime? AddedAt { get; set; }
public DateTime? ModifiedAt { get; set; }
}
I am unable to replicate this behavior. I updated and used the following sample code, that includes the GROUP_CONCAT aggregate function that you mentioned in your comment, which works without issues in Pomelo 5.0.0-alpha.2:
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
namespace IssueConsoleTemplate
{
public class IceCream
{
public int IceCreamId { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string FullName { get; set; }
}
public class Context : DbContext
{
public virtual DbSet<IceCream> IceCreams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = "server=127.0.0.1;port=3306;user=root;password=;database=So65847111_GroupConcat";
optionsBuilder.UseMySql(
connectionString,
ServerVersion.AutoDetect(connectionString),
options => options.CharSetBehavior(CharSetBehavior.NeverAppend))
.UseLoggerFactory(
LoggerFactory.Create(
configure => configure
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IceCream>(
entity =>
{
entity.Property(e => e.Name)
.HasDefaultValue("Vanilla");
entity.HasData(
new IceCream {IceCreamId = 1, Name = "Vanilla", Company = "Icecold"},
new IceCream {IceCreamId = 2, Name = "Chocolate", Company = "Mr IceCream"});
});
}
}
internal static class Program
{
private static void Main(string[] args)
{
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var iceCreamIdParameter = new MySqlParameter
{
ParameterName = "#iceCreamIdParam",
MySqlDbType = MySqlDbType.Int32,
Direction = System.Data.ParameterDirection.Input,
Value = 42, // <-- does not exist
};
var iceCreams = context.IceCreams
.FromSqlRaw(#"
SELECT `IceCreamId`, `Name`, `Company`, GROUP_CONCAT(`Name`, `Company` SEPARATOR ', ') AS `FullName`
FROM `IceCreams`
WHERE `IceCreamId` = #iceCreamIdParam
GROUP BY `IceCreamId`, `Name`, `Company`", iceCreamIdParameter)
.AsNoTracking()
.ToList();
Trace.Assert(iceCreams.Count == 0);
}
}
}
So to help you, we need more information:
The the exact LINQ query (including the exact SQL query you use in your FromSqlRaw() call)
The database server type (e.g. MySQL or MariaDB) and its exact version (e.g. 8.0.21)
(Just update the OP with the requested information.)
The exception hints, that one of your returned fields does not contain the type (or nullability) that it should:
System.InvalidCastException: Specified cast is not valid.
at MySqlConnector.Core.Row.GetInt32(Int32 ordinal)
So your query expects an Int32, but the returned value is something else.
Let your debugger break when the exception is thrown in Row.GetInt32() and examine the value it tries to cast to Int32. Then examine the ordinal parameter of the method. Finally, check the expression at the same position as the ordinal parameter in the SQL query that EF Core generated (you should find it in the Command.CommandText property of the MySqlDataReader object in the stack, or you can just log it with EF Core).
In addition to that, you can run the code I posted on your server. It should work flawlessly. If it doesn't, than this should be related to some unexpected MySQL server setting.

Exception when trying to use Mysql.Data.Entity

I'm trying to get a test project running using MySQL and EF6 using Visual Studio 2019 Community Preview for Mac. I am using the Web Application (Model-View-Controller) / .NET Core -> App template as a starting point as well as the MyWind database. I'm getting the following exception and am not sure how to proceed.
TypeLoadException: Could not load type 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetTypeDescriptor(Type type)
System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(Type type)
System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
System.Data.Entity.Internal.RetryLazy<TInput, TResult>.GetValue(TInput input)
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
System.Data.Entity.Internal.InternalContext.Initialize()
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
System.Data.Entity.Internal.Linq.InternalSet<TEntity>.Initialize()
System.Data.Entity.Internal.Linq.InternalSet<TEntity>.get_InternalContext()
System.Data.Entity.Infrastructure.DbQuery<TResult>.System.Linq.IQueryable.get_Provider()
System.Linq.Queryable.OrderBy<TSource, TKey>(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
Ef2MySql.Controllers.HomeController.Contact() in HomeController.cs ViewData["Customers"] = db.Customers.OrderBy(c => c.company).ThenBy(c => c.last_name).ThenBy(c => c.first_name).Take(10);
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Here's my relevant code.
HomeController.cs
namespace Ef2MySql.Controllers
{
public class HomeController : Controller
{
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
using (var db = new NorthwindContext())
{
ViewData["Customers"] = db.Customers.OrderBy(c => c.company).ThenBy(c => c.last_name).ThenBy(c => c.first_name).Take(10);
}
return View();
}
}
}
NorthwindContext.cs
namespace Ef2MySql.Database
{
public partial class NorthwindContext : DbContext
{
public NorthwindContext() : base("Server=localhost;Database=northwind;Uid=northwind;Pwd=northwind;")
{
}
public virtual DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
NorthwindMysqlConfiguration.cs
namespace Ef2MySql.Database
{
public class NorthwindMysqlConfiguration : MySqlEFConfiguration
{
public NorthwindMysqlConfiguration()
{
}
}
}
Customer.cs
namespace Ef2MySql.DomainObjects
{
public class Customer
{
public Customer()
{
}
public String id { get; set; }
public String company { get; set; }
public String last_name { get; set; }
public String first_name { get; set; }
public String email_address { get; set; }
public String job_title { get; set; }
public String business_phone { get; set; }
public String home_phone { get; set; }
public String mobile_phone { get; set; }
public String fax_number { get; set; }
public String address { get; set; }
public String city { get; set; }
public String state_province { get; set; }
public String zip_postal_code { get; set; }
public String country_region { get; set; }
public String web_page { get; set; }
public String notes { get; set; }
}
}
Here's the version of mono I'm using.
$ mono --version
Mono JIT compiler version 5.18.0.248 (2018-08/a4956c837e1 Fri Jan 25 16:13:12 EST 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
TLS:
SIGSEGV: altstack
Notification: kqueue
Architecture: amd64
Disabled: none
Misc: softdebug
Interpreter: yes
LLVM: yes(600)
Suspend: preemptive
GC: sgen (concurrent by default)
I'm not sure if this is relevant, but for the MySql.Data.Entity and EntityFramework packages, I'm getting the following warning:
Package 'EntityFramework 6.2.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.
For what it's worth, I am able to use MySql.Data.MySqlClient.MySqlConnection directly to retrieve data from the database.
After doing some research, it seems that this class is simply missing from corefx. It has been added for 3.0.

Json.Net: specifying object type when populating an interface property

I have an object that implements an interface with a property that is another interface:
public interface IMaster
{
ISlave ObjectProp {get; set; }
}
public interface ISlave
{
string Name {get; set; }
}
...
public class Master : IMaster
{
public ISlave ObjectProp {get; set; }
}
public class Slave : ISlave
{
public string Name {get; set; }
}
I can create an instance of the Master class and deserialize it using JsonConvert.PopulateObject.
When deserializing the property, however, the parser cannot, obviously, create an instance of an interface.
Is there a way to tell the Json library (via attribute, callback etc.) to create an instance of the Slave class (that implements the interface in question)?
Yes, this looks like a duplicate - JsonConverterAttribute appears to work fine. For completeness sake, below is the solution:
...
public class Master : IMaster
{
[JsonConverter(typeof(ModelJsonConverter), typeof(Slave))]
public ISlave ObjectProp {get; set; }
}
...
public class ModelJsonConverter : JsonConverter
{
public Type InstanceType { get; set; }
public ModelJsonConverter(Type instanceType):base()
{
InstanceType = instanceType;
}
public override bool CanWrite => false;
public override bool CanRead => true;
public override bool CanConvert(Type objectType)
{
if (objectType == InstanceType) return true;
else return InstanceType.GetTypeInfo().Is​Subclass​Of(objectType);
}
public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
}
public override object ReadJson(JsonReader reader,
Type objectType, object existingValue,
JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
else if (reader.TokenType != JsonToken.StartObject) throw new JsonException($"Unexpected Json token at {reader.Path}: {reader.TokenType.ToString()}");
var deserialized = Activator.CreateInstance(InstanceType);
serializer.Populate(reader, deserialized);
return deserialized;
}
}

Json Deserialization on SSIS

I'm quite new to JSON and would like to ask your help.
I'm trying to deserialize Json into a object class but whenever I call JavaScriptSerializer.Deserialize I get this error:
Error: 0x1 at Data Flow Task, Error Getting Data From Webservice!:
System.MissingMethodException: No parameterless constructor defined
for type of 'CurrencyOut[]'. at
System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2
dictionary, Type type, JavaScriptSerializer serializer, Boolean
throwOnError, Object& convertedObject)
Here's the Json source:
{"terms":"http://www.xe.com/privacy.php","privacy":"http://www.xe.com/legal/dfs.php","to":"IDR","amount":100.0,"timestamp":"2015-06-01T16:00:00Z","from":[{"quotecurrency":"USD","mid":0.0075713171}]}
Here's my class:
class CurrencyOut
{
public string Tems { get; set; }
public string Privacy { get; set; }
public CurrArr From { get; set; }
public decimal Amount { get; set; }
public DateTime Timestamp { get; set; }
public string To { get; set; }
}
class CurrArr
{
public string QuoteCurrency { get; set; }
public decimal Mid { get; set; }
}
Thanks

MvvmCross: Deserilization Error for JSON

I'm trying to create a simple application through Lesson 6 in N+1 Days of MvvmCross application sample. But its failed in SimpleRestService while converting json Data serialization.
private T Deserialize<T>(string responseBody)
{ // Error is here for deserilizing
var toReturn = _jsonConverter.DeserializeObject<T>(responseBody);
return toReturn;
}
My Json data through Browser:
[{"Desc":"All","Id":"0"},{"Desc":"Assigned","Id":"2"},{"Desc":"In Progress","Id":"3"},{"Desc":"Resolved","Id":"4"},{"Desc":"Closed","Id":"5"},{"Desc":"Hold","Id":"6"},{"Desc":"低","Id":"8"},{"Desc":"Waiting Approval","Id":"9"},{"Desc":"Cancelled","Id":"10"},{"Desc":"Not Resolved","Id":"8"}]
My Json data in application at responsebody:
[{\"Desc\":\"All\",\"Id\":\"0\"},{\"Desc\":\"Assigned\",\"Id\":\"2\"},{\"Desc\":\"In Progress\",\"Id\":\"3\"},{\"Desc\":\"Resolved\",\"Id\":\"4\"},{\"Desc\":\"Closed\",\"Id\":\"5\"},{\"Desc\":\"Hold\",\"Id\":\"6\"},{\"Desc\":\"低\",\"Id\":\"8\"},{\"Desc\":\"Waiting Approval\",\"Id\":\"9\"},{\"Desc\":\"Cancelled\",\"Id\":\"10\"},{\"Desc\":\"Not Resolved\",\"Id\":\"8\"}]
Error Message shows as :
{Newtonsoft.Json.JsonSerializationException: Cannot deserialize JSON array (i.e. [1,2,3]) into type 'Book.Core.Services.BookSearchResult'.
The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList.
To force JSON arrays to deserialize add the JsonArrayAttribute to the type. Path '', line 1, position 1.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in :0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String reference) [0x00000] in :0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, System.Object existingValue) [0x00000] in :0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.JsonConverter converter, Newtonsoft.Json.Serialization.JsonContainerContract containerContract) [0x00000] in :0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0
at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0
at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 etc.. }
My Code part:
Class Declaration:
public class BookSearchItem
{
public string Desc { get; set; }
public string Id { get; set; }
}
public class BookSearchResult
{
public List<BookSearchItem> items { get; set; }
}
Binding Declaration:
public void StartSearchAsync(string whatFor, Action<BookSearchResult> success, Action<Exception> error)
{
string address = string.Format("http://192.168.0.76/eFACiLiTYPhone/MobileService/WinPhoneWCFService.svc/callstatustesting");
_simpleRestService.MakeRequest<BookSearchResult>(address,"GET", success, error);
}
Simple Rest Service For Common:
public class SimpleRestService :ISimpleRestService
{
private readonly IMvxJsonConverter _jsonConverter;
public SimpleRestService(IMvxJsonConverter jsonConverter)
{
_jsonConverter = jsonConverter;
}
public void MakeRequest<T>(string requestUrl, string verb, Action<T> successAction, Action<Exception> errorAction)
{
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = verb;
request.Accept = "application/json";
MakeRequest(
request,
(response) =>
{
if (successAction != null)
{
T toReturn;
try
{
toReturn = Deserialize<T>(response);
}
catch (Exception ex)
{
errorAction(ex);
return;
}
successAction(toReturn);
}
},
(error) =>
{
if (errorAction != null)
{
errorAction(error);
}
}
);
}
private void MakeRequest(HttpWebRequest request, Action<string> successAction, Action<Exception> errorAction)
{
request.BeginGetResponse(token =>
{
try
{
using (var response = request.EndGetResponse(token))
{
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream);
successAction(reader.ReadToEnd());
}
}
}
catch (WebException ex)
{
Mvx.Error("ERROR: '{0}' when making {1} request to {2}", ex.Message, request.Method, request.RequestUri.AbsoluteUri);
errorAction(ex);
}
}, null);
}
private T Deserialize<T>(string responseBody)
{
var toReturn = _jsonConverter.DeserializeObject<T>(responseBody);
return toReturn;
}
}
You have to use the correct T for your Json call - you can't simply use BookSearchResult for all Json calls.
You can use tools like http://json2csharp.com/ to generate the CSharp classes for you - e.g.
public class RootObject
{
public string Desc { get; set; }
public string Id { get; set; }
}
which you can then use as:
var myItems = service.Deserialize<List<RootObject>>(jsonText);