Prevent Json.NET from interpreting a string as a date - date

I have some attributes returned from a rest service that are served as an array of name-value pairs.
In some cases the value is a date expressed in universal sortable format:
{
"name": "Modification-Date",
"value": "2017-11-13T15:15:13.968Z"
}
When it gets parsed by the deserialiser, the value is identified as a date but given that the object the pair is deserialised into has type string for both name and value, the date is then converted to string and it loses precision: "13/11/2017 15:15:13"
This is easily visible by using a converter for the NameValue type.
if (reader.TokenType == JsonToken.StartObject)
{
var item = JObject.Load(reader);
return new NameValueFacet()
{
Name = item["name"].Value<string>(),
Value = item["value"].Value<string>()
};
}
item["value"].Type shows the type is Date.
How do I get to have Json.NET leave it as a string, "unparsed"?

You can try with Newtonsoft. See below.
JsonConvert.DeserializeObject<your_object>(your_json, new IsoDateTimeConverter{ DateTimeFormat = "dd/MM/yyy" });

Related

The element type 'int' can't be assigned to the map value type 'FieldValue' when trying to assign a new value

I have initial data which works fine.
var data = {field1: FieldValue.increment(1)};
And it is also fine when I add another field to the data.
data.addAll({field2: FieldValue.increment(1)});
But if I set the value to 0, it won't allow me to.
data.addAll({field3: 0});
It will give an error of:
The element type 'int' can't be assigned to the map value type 'FieldValue'.
I tried doing this but still, have the same issue.
data[field3] = 0;
How will I set the field3 to a specific value?
Note:
This is the full code.
DocumentReference<Map<String, dynamic>> ref = db.collection('MyCollect').doc(uid);
var data = {field1: FieldValue.increment(1)};
data.addAll({field2: FieldValue.increment(1)});
data.addAll({field3: 0});
ref.set(data, SetOptions(merge: true));
For a better understanding
you can use the var keyword when you don't want to explicitly give a type but its value decides its type, and for the next operations/assignments, it will only accept that specific type that it took from the first time.
On the other hand, the dynamic keyword is used also to not explicitly set a type for a variable, but every other type is valid for it.
var a = "text";
a = "text2"; // ok
a = 1; // throws the error
dynamic b = "text";
b = "text2"; // ok
b = 1; // also ok
in your case, you're using the var keyword, so in the first value assignment it takes its type:
var data = {field1: FieldValue.increment(1)}; // takes the Map<String, FieldValue> type
data.addAll({field3: 0}); // 0 is int and FieldValue.increment(1) is FieldValue type, so it throws an error
However, you can fix the problem and let your data variable accept any kind of element types by either using the dynamic keyword:
dynamic data = {field1: FieldValue.increment(1)}; // will accept it.
or, specifying that this is a Map, but the values of it are dynamic:
Map<String, dynamic> data = {field1: FieldValue.increment(1)}; // will accept it also.
Hope this helps!
check your dart type.
Difference between "var" and "dynamic" type in Dart?
the type var can't change type of variable. so check your code.
var data = {field1: FieldValue.increment(1)};
maybe data's type fixed something <String, FieldValue>
you can try dynamic type.

Apollo/GraphQL: Field Type to Use for Timestamp?

I'm storing a value to a postgres field that is of type timestamp with time zone. I was defining the field as an int in my Apollo schema, but I'm getting this error message in the resolver:
column "apptDateTime" is of type timestamp with time zone but expression is of type integer
Looking up GraphQL data types, I don't yet see any type that is cited as corresponding to a field of type timestamp.
What's the correct field type to use in the Apollo schema for a field that is of type timestamp in the database?
I find this way to work with input in forms, needed convert from client (input form) to the server, and from the server to client (input form)
Graphql:
updatedAt: String
Sequelize:
updatedAt: { type: Sequelize.DATE },
Postgresql:
"createdAt" timestamp(6) with time zone DEFAULT now(),
Value transform to the Server:
value = dateToISO(value);
Value transform to the Client:
if ( isNil(value) ) {
value = '';
} else {
value = value.toLocaleDateString() +' '+ value.toLocaleTimeString();
}
the helpers:
let dateToISO = function (dateString) {
if (!dateString) {
return null;
}
let p = dateString.split(/\D/g);
/* It's up your date on input in this case come from DD-MM-YYYY
for MM-DD-YYY use: return [p[1], p[2], p[0]].join('-'); */
return [p[2], p[1], p[0]].join('-');
};
I got my mutation working that includes a field of type Timestamp with Time Zone. I did it by changing the schema for my apptDateTime field from Int to String, and passing in an ISO string. Sequelize and Postgres took care of changing it into a field of type Timestamp with Time Zone.
Update 2021:
Here's what I'm using these days.
Sequelize:
timeOfNonce: {type: Sequelize.DATE}
Schema:
scalar DATETIME
.....
timeOfNonce: DATETIME
These days I let Sequelize define my SQL tables via:
const deleteAllData_fromThisModel = false;
const alterThisTableToMatchDBConnectorsModel = true;
myDataModel.sync({force: deleteAllData_fromThisModel,
alter: alterThisTableToMatchDBConnectorsModel}).then(err => {
console.log('myDataModel has been synced')
}).catch(err => {
throw err
});

How to convert String to Integer from OData-Model in SAPUI5?

I have an OData model with some Edm.String fields which represents ABAP NUMC(9) or NUMC(10). All other Edm-Types (like Int32) ends with an error.
Can I now convert these Fields to Integer in SAPUI5? The content is i.e. 0000012345. If I use type in my binding, my text is empty. If I use it without type, the strings are output correctly.
new sap.m.Text({
text: {
path: "{statusData>AnzPdf}",
type: new sap.ui.model.type.Integer()
}
})
Apply formatter as give in example to convert data from model to Integer format.

Issue with passing a value to Date Object

I am want to display a date in MM/yyyy format. I am using the below code to change the format :
var inputDate = new Date(data);
var date = dojo.date.locale.format(inputDate, {datePattern: "MM/yyyy", selector: "date"});
data contains the input date. For example when German Locale is set in the browser, the input value is like : 01.03.2016 05:30
while creating the date object with this value gives invalid Date though it works when the US locale is set in the browser.Please guide to fix this.
You can use locale.parse to convert your localized date string into a date object, then convert the date object into the formatted date you want.
See this small example:
var browserLocale = 'de',
data = '01.03.2016 05:30';
require(["dojo/i18n", "dojo/date/locale"], function(i18n, locale){
require([i18n.getL10nName("dojo/cldr", "gregorian", browserLocale)], function() {
var dateObject = locale.parse(data, {formatLength: 'short', locale: 'de'});
alert(locale.format(dateObject, {datePattern: "MM/yyyy", selector: "date"}));
});
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>

Conversion of a object into a particular datetime format

I want to convert a query string object into a datetime in this format:- "YYYY-mm-dd HH:mm:ss.xxx" in C#.net. But when i am using Convert.ToDateTime(object) for getting the datetime value then an exception is being fired.
Could anyone can provide me the iFormatProvider for the same.?
Thanks
Varun Sareen
Have a look at DateTime.TryParseExact Method
I think your problem is trying to convert the QueryString object, instead of getting a value from the query string and then converting the value.
A QueryString object is a keyed collection of the values specified in a URL from an HTTP request. So if you have a URL like: http://example.com?a=1&b=2&c=3, the request QueryString object will contain three values: 1, 2, and 3. To access the values you would use the keys:
var aValue = Request.QueryString["a"];
And the variable aValue would then contain the string value "1" (without the quotes).
After getting the value from the query string, you can use the TryParseExact method suggested by #astander.