Serialize and Deserialize JSON with different property names for different use cases - json.net

I have data stored in a database with short field names to save space. When I load these into my .NET Core app, I convert the data to its C# model. Then, when I pass this to the fronted I want to serialize the JSON to have full field names. So instead of "fN", "firstName".
I know I can use [JsonProperty(PropertyName = "SomeName")] to change serialization/deserialization. What I'm asking is whether there's a way to specify multiple property names for a single field, ideally with some additional "profile" variable so that when I serialize/deserialize from the DB I use the DB profile, and when sending to the client I use the other profile. This way I can either use the short or long names when convenient.
Thanks!

Related

Handling Json Objects from SQL server DB at Fastreport

its my first time using fastreport, i am using FR with a asp.net web api, after searching i have decieded to get the data from db using stored proc. and get whatever pass whatever filter im using from the report from the front-end to the api (example like a certain Id of the object i wanna create a report for) and then pass this value to the stored proc. i export to the FR and pass this value to my parameters, i don't know if this is the best way to use FR with my api so i'm open to any suggestions. Now my main problem is that i have json obj in my SQL DB for localization(ex: the Name obj is stored like that {"en":"TestAcc2","fr":"test"}) so how can i convert a certain language from a field like this to show in the report, i have found resources on how to deal with a whole json DB but i only want to deserlizae this field so any help on how i could do it ?

Set a SQL Server database column's data type to a class

I recently made a monopoly game using C# Winforms and now I need to convert it to a web application. I'm using ASP.NET and I'm having some issues with variables being reset on button clicks.
In order to resolve this, I was planning on having all data stored within a SQL Server database whenever it is changed, and then retrieved when needed. The main issue is that I have 2 classes, Square and Player.
I can handle the data for player fine but each square has a Player associated with it called "Owner". I'm just wondering if there is any way that in the SQL Server database I can set the data type for "Owner" to the "Player" class so that I can save that along with all the other data. Thanks in advance.
SQL Server was designed for and works best for structured tabular data. For complex objects, this usually means it takes several records over several tables to represent a single object.
However, what you are trying to do is store a complex object in a single column in SQL Server. You can do this in SQL Server, by serializing your object to a string (usually XML or JSON) and storing the string representation of your object in an nvarchar(max) column in SQL. When you fetch the record, deserialize it back into an object. You lose quite a bit of power in SQL Server when doing this, but if you don't need to manipulate the data within your object server-side, then it works fine (you can do limited XML and JSON processing within SQL server, but from the sounds of what you are doing, you won't need to).
One of the easiest and most popular ways to serialize objects is to use Newtonsoft JSON. Add the NuGet package for Newtonsoft, and then serialize your objects by calling string myComplexObjectAsAString = JsonConvert.SerializeObject(myComplexObject);. You can send that string to SQL, and when you get it back, use MyComplexObjectType myComplexObject = JsonConvert.DeserializeObject<MyComplexObjectType>(theStringDataThatYouGotFromSqlServer);

Marketo REST API - what is "dedupeFields" for custom objects?

When it comes to creating/updating custom objects, can I use both dedupeFields or lookupField when pushing the data to Marketo?
What is the difference between the two?
I'm not sure what do you mean under lookupField, as there is no such input field described in the API documentation of the Sync Custom Objects endpoint. (That is the endpoint to create or update custom objects.)
In the other hand, you do not need such a standalone lookup field, as with the input array you provide the objects you want to create or update, with all their important values. Have a look at the sample payload in the docs.
When input is used together with the optional dedupeBy and action fields, you have full control over which object you want to create or update.
Also, the endpoint expects the name of the dedupe field under dedupeBy key, as opposed to dedupeFields. So the name is singular; you can provide a single field name use, and it does what you can expect: if the value in the field for a given record is not unique, an error will be returned for the individual record.

ASP.NET: Best way to determine type of object serialized to XML

I have a table in my SQL Server DB that holds auditing information for certain actions a user takes within my system. Things like who performed the action, when it was performed, and what action are all pieces of information that can easily span multiple actions. But depending on the action performed, there may be other information that I want to capture, that is specific to the action. To handle this, I elected to add an "XML Metadata" column to the table that holds serialized XML of different metadata objects that I've created. I created a metadata object for each of the actions that I'm interested in tracking extra for. So each object is responsible for tracking specific extra information (metadata) for it's action. The objects are serialized and written to my new column.
I have SystemAction objects that I use to store information from this table, and I've added a string field that holds the XML string from the DB. The problem is, when I'm reading this XML back from the SystemAction objects, I'm struggling with a way to generically translate it back into it's correct metadata object. Each metadata object is going to have different fields, and each object has it's own static method that takes an XML string and attempts to return the metadata object type. So I could say:
SomeActionMetadata mdObj = SomeActionMetadata.BuildFromXML(xmlStringFromDB);
But I really don't know of a way to say "Here's some XML that could translate to any number of different objects. Figure it out and give me the right object back."
Given my current implementation, I could always just assign a unique ID to each metadata object that is stored as a field in each object, then use a case statement to switch on that ID and use the appropriate class's static build method to build the right object. But I was hoping for something a little more automatic than that. What if I have a List of SystemAction objects and just want to loop through them and generate the correct metadata object type?
I was hoping someone might have run across something similar to this before, or could point me to an article or post that could help me out. Thanks very much.
As indicated by Subhash Dike in the comments below, there is a similar SO question here that was able to point me in the right direction.

Entity Framework - Mapping doesn't map for stored procedure?

I have a stored procedure that search a view using full text.
I'm trying to map the results to an existing Entity (Sales), where I mapped the column MivType to SaleType (as it makes more sense, and I want to keep db names away from my web site). The stored procedure is mapped to a Function Import, and I've defined its ReturnType to Sales.
This work well as long as the entity has the same property names as fields names.
Here's my problem: when I change the property's name, I get the following error after running the imported function:
The data reader is incompatible with the specified 'Model.Sale'. A member of the type, 'SaleType', does not have a corresponding column in the data reader with the same name.
I can fix this if I change the property 'SaleType' to 'MivType' on the entity, but why should I do that? Isn't that what the mapping is for?
This means I have to use the exact same names on the stored procedure and the entity, so in effect, the mapping is ignored (I have names like YzrName, MivYaad, etc, and I don't like it).
Is there a simple way around this? I don't want to use the db names on my application, and prefer not to change the stored procedure...
(I should mention I'm a beginner with the EF, so this can be a rookie mistake)
Thanks.
Well the entity designer doesnt work very well. I generally try to do everything in the XML. In the XML there are 3 parts. The Storage (a representation of the SQL Database). The Conceptual (a represention of your .Net Objects. and the Conceptual to Storage Mapping
It sounds like the error is in your Conceptual to Storage Mapping. You can keep the property name SalesType on the conceptual side but the mapping must map the the correct names on both the conceptual and storage side.
Refer to MSDN here are some articles
http://msdn.microsoft.com/en-us/library/cc716731.aspx

Resources