An Error Occurred While serializing to JSON - json

Hi firends i'm getting error when i tried to Serialize JSON. I have a model and it have List property;
MyModel:
public virtual int Kod { get; set; }
public virtual string Ad { get; set; }
public virtual string TrafikKod { get; set; }
public virtual int TapuKod { get; set; }
public virtual bool AktifMi { get; set; }
[JsonIgnore]
public virtual IList<TapuIlceModel> IlceList { get; set; }
public TapuIlModel()
{
IlceList = new List<TapuIlceModel>();
}
MyApiController:
[Route("api/TapuZeminApi/GetZemins")]
[HttpPost]
public string GetZeminsFromZeminArg(object arg)
{
ZeminArg zemArg = SConvert.DeserializeJSON<ZeminArg>(arg.ToString());
List<TapuZeminModel> zeminList = TapuModule.GetZeminListFromArgs(zemArg);
string jsonResult = SConvert.SerializeJSON(zeminList);
return jsonResult;
Here is a picture about my reciving data error;
And this my inner exc.;
.{"Error getting value from 'ReadOnly' on 'NHibernate.Proxy.DefaultLazyInitializer'."}
.{"Proxy is detached (i.e, session is null). The read-only/modifiable setting is only accessible when the proxy is associated with an open session."}
What should i do? I need all data... Thanks
when i check again i found something like that:
Why is there 2 base??? And why name is different? Anyone know?

I geting same error when serilize SerializeJSON(object) , i have try JsonConvert.SerializeObject with JsonSerializerSettings and ContractResolver, it will work.
string jsonResult = JsonConvert.SerializeObject(object, new JsonSerializerSettings() {
ContractResolver = new NHibernateContractResolver()
});

Related

How to get certain properties of the array from Model to ViewModel without Json parseerror (Asp.Net Core MVC)

I'm currently working on the Asp.Net Core MVC project and have the following Message class
public class Message
{
public int Id { get; set; }
public string SenderId { get; set; }
[ForeignKey("SenderId")]
public virtual User Sender { get; set; }
public string RecipientId { get; set; }
[ForeignKey("RecipientId")]
public virtual User Recipient { get; set; }
public string Content { get; set; }
public bool IsRead { get; set; }
public DateTime? DateRead { get; set; }
public DateTime MessageSent { get; set; }
public bool SenderDeleted { get; set; }
public bool RecipientDeleted { get; set; }
}
Using the mapper, I get the below ViewModel for the Message review:
public class MessageReviewViewModel
{
public int Id { get; set; }
public string SenderId { get; set; }
[ForeignKey("SenderId")]
public virtual User Sender { get; set; }
public string RecipientId { get; set; }
[ForeignKey("RecipientId")]
public virtual User Recipient { get; set; }
public string Content { get; set; }
public bool IsRead { get; set; }
public DateTime? DateRead { get; set; }
public DateTime MessageSent { get; set; }
}
Now, I would like to get actually the Recipient and Sender users, however the reason I have such a simple MessageReviewViewModel is because I'm getting the list of this view models and with this users json throws an error & without it I have successfull result.
However, the problem is, that for the proper display in the Inbox view I still need the certain properties of Recipient and Sender user's (their main photo url, username & etc).
My mapper configuration is as below to get the messages from the repository:
public async Task<IEnumerable<MessageReviewViewModel>> GetMessagesForUserByUserId(string userId)
{
var messages = await messageRepository.GetMessagesForUser(userId);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Message, MessageReviewViewModel>();
cfg.IgnoreUnmapped();
});
IMapper mapper = config.CreateMapper();
var messageList = mapper.Map<IEnumerable<Message>, IEnumerable<MessageReviewViewModel>>(messages);
return messageList;
}
In the react component (I have integrated react.js in the asp.net core mvc) once it did mount I make a get request to get the messages list as below and setState messagesList to the received array.
componentDidMount() {
const xhr = new XMLHttpRequest();
xhr.open('get', "/Messages/GetMessages", true);
xhr.onload = () => {
const data = JSON.parse(xhr.responseText);
this.setState({ messagesList: data });
console.log(this.state.messagesList);
};
xhr.send();
}
And this is the action in the controller, that is being called:
[HttpGet]
public async Task<IActionResult> GetMessages()
{
var userFromRepo = await userManager.FindByNameAsync(User.Identity.Name);
var messages = await messagesService.GetMessagesForUserByUserId(userFromRepo.Id);
var sortedMessageList = messages.OrderByDescending(m => m.MessageSent);
return Json(sortedMessageList);
}
As I mentioned, it all works without any problem unless there are no virtual User Sender and virtual User Recipient in the MessageReviewViewModel. Once I have them in code, this is the error I get:
Probably it is worth to mention, that the User class objects (in my scenario Sender & Recipient for example) also have the virtual properties inside and I thought that it may be problem for Json parse these objects, which have other objects as property.
Could you please advise how I can include these properties in the ViewModel so neither mapper nor JSON throw any error? I'm okay even to get only selected properties of the obje (for example just string username, mainphoto url & etc).
It is also ok, if there is any Json method, that will solve this parseError with virtual users included in the ViewModel
I figured it out after finding that mapper can also be configured to the level that is required by the user
The Message model is as below as previously
public class Message
{
public int Id { get; set; }
public string SenderId { get; set; }
[ForeignKey("SenderId")]
public virtual User Sender { get; set; }
public string RecipientId { get; set; }
[ForeignKey("RecipientId")]
public virtual User Recipient { get; set; }
public string Content { get; set; }
public bool IsRead { get; set; }
public DateTime? DateRead { get; set; }
public DateTime MessageSent { get; set; }
public bool SenderDeleted { get; set; }
public bool RecipientDeleted { get; set; }
}
And the MessageReviewViewModel changed to below:
public class MessageReviewViewModel
{
public int Id { get; set; }
public string SenderId { get; set; }
public string SenderUsername { get; set; }
public string SenderMainPhotoUrl { get; set; }
public string RecipientId { get; set; }
[ForeignKey("RecipientId")]
public string RecipientUsername { get; set; }
public string RecipientMainPhotoUrl { get; set; }
public string Content { get; set; }
public bool IsRead { get; set; }
public DateTime? DateRead { get; set; }
public DateTime MessageSent { get; set; }
}
Only for the mapper the configuration was required as below:
public async Task<IEnumerable<MessageReviewViewModel>> GetMessagesForUserByUserId(string userId)
{
var messages = await messageRepository.GetMessagesForUser(userId);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Message, MessageReviewViewModel>()
.ForMember(destination=>destination.RecipientUsername,map=>map.MapFrom(
source=>source.Recipient.UserName)) //gets only string for username instead of whole User model
.ForMember(destination=> destination.RecipientMainPhotoUrl,map=>map.MapFrom(
source=>source.Recipient.MainProfilePicture)) //gets only string for profile picture instead of whole User model
.ForMember(destination=>destination.SenderUsername,map=>map.MapFrom(
source=>source.Sender.UserName))
.ForMember(destination=>destination.SenderMainPhotoUrl,map=>map.MapFrom(
source=>source.Sender.MainProfilePicture));
cfg.IgnoreUnmapped();
});
IMapper mapper = config.CreateMapper();
var messageList = mapper.Map<IEnumerable<Message>, IEnumerable<MessageReviewViewModel>>(messages);
return messageList;
}
As all received data now are only strings, Json has no problem parsing it

REST API returns "bad array" instead of JSON object

I'm building REST API server in .NET core. I'm testing my code via Postman software. I have a problem with Include() method that enables me to attach navigation property data. I'm trying to get data in [HttpGet] action and objects that are being returned are wrong.
My code :
MODELS
Session model
public class Session
{
[Key]
public int IDSession { get; set; }
[Required]
public DateTime LogInTime { get; set; }
public DateTime LogOutTime { get; set; }
[Required]
public int IDUser { get; set; }
public User User { get; set; }
[Required]
public int IDMachine { get; set; }
public Machine Machine { get; set; }
}
User model
public class User
{
[Key]
public int IDUser { get; set; }
[Required]
public string Forename { get; set; }
[Required]
public string Name { get; set; }
public string AvatarPath { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Password { get; set; }
public User CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
public List<UserGroup> UsersGroups { get; set; }
public List<Alarm> ExecutedAlarms { get; set; }
public List<Alarm> ResetedAlarms { get; set; }
public List<AccessCard> Cards { get; set; }
public List<AccessCard> UserCardsAdded { get; set; }
public List<User> UsersAdded { get; set; }
public List<Session> Sessions { get; set; }
public List<EventsLog> Events { get; set; }
public List<Reference> References { get; set; }
public List<UserPermission> UsersPermissions { get; set; }
}
Session controller
[Produces("application/json")]
[Route("api/Sessions")]
public class SessionsController : Controller
{
private readonly DBContext _context;
#region CONSTRUCTOR
public SessionsController(DBContext context)
{
_context = context;
}
#endregion
#region HTTP GET
// GET: api/sessions
[HttpGet]
public async Task<IActionResult> GetSessions()
{
var sessions = await _context.Sessions.Include(s => s.User). ToListAsync();
if (sessions.Any())
{
return new ObjectResult(sessions);
}
else
{
return NotFound();
}
}
// GET: api/sessions/1
[HttpGet("{id}", Name = "GetSessionByID")]
public async Task<IActionResult> GetSessionByID(Int32 id)
{
var session = await _context.Sessions.Include(s => s.User).FirstOrDefaultAsync(s => s.IDSession == id);
if (session == null)
{
return NotFound();
}
else
{
return new ObjectResult(session);
}
}
#endregion
}
The idea is that User model contains List<Session> collection that he/she created. I want to be able to return users with its sessions
Of course Session model contains a single User because every session is related with a specific, single User.
Now, when I need to get all sessions objects in SessionController with GetSessions() or GetSessionsByID() I use POSTMAN [HttpGet] action like this : http://localhost:8080/api/sessions which returns me wrong data :
A session contains a user and in turn a single user is related with its sessions. It looks like it tries to return me Session object properly, includes User object but then it tries to include all sessions for that User. That's not what I want. It looks like some kind of a loop. Sessions shoud be returned with its User objects and that's it. How can I achieve that ? Am I doing some logical mistake in my models ?
Thanks !
I met also this issue recently. So, I've fixed it by adding this script in the Startup.cs file and ConfigureServices method :
services.AddMvc().AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
So, you suffix services.AddMvc() by this code who means that you have to make JSON.Net to ignore cycles finded to the nested object request. And of course having Newtonsoft.Json package installed to your project and referenced in each concerned file
For much clearer information, see this link at Related Data and Serialization section :
https://learn.microsoft.com/en-us/ef/core/querying/related-data
Hope this is helpfull for you

Azure route not forwarded to endpoint after filtering query

I am starting to use Azure IoT hub and I configured my endpoints and servicehub to learn from it with a route.
In this route I specify that when a message says level = critical that it forwards the message to my endpoint like explained in the following link: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-process-d2c
The difference is that I use my own code for my temperature sensor with a programmed DTO and that I send it as one big json message (see code)
DTO class:
[DataContract]
class Bmp280DTO
{
[DataMember]
public Guid guid { get; set; }
[DataMember]
public string deviceName { get; set; }
[DataMember]
public float tempSensorValue { get; set; }
[DataMember]
public float pressureSensorValue { get; set; }
[DataMember]
public float altitudeSensorValue { get; set; }
[DataMember]
public DateTime measurementTime { get; set; }
[DataMember]
public string measurename { get; set; }
[DataMember]
public string level { get; set; }
public Bmp280DTO() { }
public Bmp280DTO(Guid id, String Device, float TmpSensorValue, float PrSensorValue, float AlSensorValue)
{
this.guid = id;
this.deviceName = Device;
this.tempSensorValue = TmpSensorValue;
this.pressureSensorValue = PrSensorValue;
this.altitudeSensorValue = AlSensorValue;
this.measurementTime = DateTime.Now;
this.measurename = "LightSensor";
this.level = DetermineMessageLevel(TmpSensorValue);
}
public string ToJson()
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Bmp280DTO));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, this);
string json = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length);
return json;
}
public string DetermineMessageLevel(float temperaturesensorvalue)
{
if(temperaturesensorvalue > 22)
{
return "critical";
}
return "normal";
}
}
Message send:
private void SendBmp280Data(Bmp280DTO AzureBmp280Data)
{
System.Text.StringBuilder Bmp280JsonMessage = new System.Text.StringBuilder();
Bmp280JsonMessage.Append(AzureBmp280Data.ToJson());
MessageCommands.SendMessage(Bmp280JsonMessage.ToString());
}
Examples of message:
{"guid":"xxxx","deviceName":"Bmp280Sensor","tempSensorValue":23.59763,"pressureSensorValue":98792.11,"altitudeSensorValue":213.046539,"measurementTime":"2017-02-23T05:34:00.4544388-08:00","measurename":"LightSensor","level":"critical"}
The message is received in the IOT hub but not forwarded to the endpoint.
What am I doing wrong? Are you not able to query json object messages? And if that's the case how do you do it then? I don't want to send unnecessary messages.
It seems that this was a problem with the encoding while sending the message.
I encoded the message in UTF8 while it needed to be done in ASCII value's.
I also could add message-properties to make it more obvious.
So as following:
var messageString = JsonConvert.SerializeObject(AzureBmp280Data);
Debug.WriteLine("Message Sent: {0}", messageString, null);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
message.Properties.Add("level", "critical");

Web API Json Serialization Exception & Dynamic Entities

I have a web API controller method that is returning an object which is giving the client a 500 internal server error. On the server, the output windows says "Newtonsoft.Json.JsonSerializationException". But I cannot see anything wrong with the class I am returning.. and I am sure this has historically been working. Any help would be greatly appreciated!
EDIT: Is this a problem with the web API not being able to serialize a 'dynamic entity'? The code that generates the class is here:
var id = User.Identity.GetUserId();
var user = db.Users
.Where(u => u.Id == id)
.Include(u => u.Friends)
.FirstOrDefault();
return user;
I am returning the following class;
public class User : IdentityUser
{
public User()
{
this.Friends = new List<UserFriend>();
}
public string PhoneNumber { get; set; }
public string Email { get; set; }
public List<UserFriend> Friends { get; set; }
public bool HasRegistered { get; set; }
public string LoginProvider { get; set; }
}
The 'UserFriend' class looks like this;
public class UserFriend
{
public int UserFriendId { get; set; }
public string Id { get; set; }
public string FriendUserId { get; set; }
public string FriendUserName { get; set; }
public string FriendPhoneNumber { get; set; }
}
Strangely, when I hover over the returned object on the server, the type is: {System.Data.Entity.DynamicProxies.User_7283E76A736B4DD47E89120E932CD5C04B62F84C316961F02CDAE3EEF4786504}. I am not sure what this is.. :-O
I used AutoMapper to create a DTO instead of just returning the User class. The DynamicProxies class is because the query uses lazy loading and it has not got the object yet.
After installing automapper (Install-Package AutoMapper);
Mapper.CreateMap<User, UserDto>();
UserDto dto = Mapper.DynamicMap<UserDto>(user);
Then return the dto.

Web Api won't serialize my Object Graph

I got two simple entities:
public class Ingredient : IEntity
{
public Ingredient()
{
Drinks = new List<Drink>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Drink> Drinks { get; set; }
}
public class Drink : IEntity
{
public Drink()
{
Ingridients = new List<Ingredient>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Ingredient> Ingridients { get; set; }
public string Approach { get; set; }
}
I get the following error:
Object graph for type 'Gudo.Core.Model.Ingredient' contains cycles and cannot be serialized if reference tracking is disabled.
I've tried using the JsonIgnore Attribute on the Drinks collection and I've tried using:
JsonSerializerSettings jsSettings = new JsonSerializerSettings();
jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
In my global.asax
Nothing works..
Please help.
Did you make sure to set this on the JSON formatter's serializer settings? This line should do it for you:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;