如何使用Newtonsoft.Json将DateTimeOffset属性序列化为(UTC)毫秒?

时间:2019-07-08 02:08:56

标签: c# json json.net

请注意,这与时间转换无关,而与转换JSON / C#类型有关

我需要序列化和反序列化具有属性DateTimeOffset的对象,但是序列化时她的值需要以毫秒为单位;反序列化时,再次将其转换为DateTimeOffset。 我到了这一点:

methods: {
  getTagNames: (tags) => {
    return tags.map(tag => tag.name)
  }
}

但是我现在不知道如何在毫秒内进行序列化。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

使用method from this answer,我们可以获得相应的Unix毫秒纪元:

import os
import cv2
import keras
import numpy as np

from keras.models import load_model
new_model = load_model('/path/to/mymodel.h5')

test_dir = '/path/to/test-set'
test_imgs = ['/path/to/test-set/{}'.format(i) for i in os.listdir(test_dir)]

def read_and_process_image(list_of_images):

  X = []

  for image in list_of_images:
      img = cv2.imread(image, cv2.IMREAD_COLOR)
      X.append(cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), 
              (299, 299), interpolation=cv2.INTER_CUBIC))

  return X

X_test = read_and_process_image(test_imgs)
x = np.array(X_test)
test_datagent = ImageDataGenerator(rescale=1./255)

class_names = ['neg','pos']
i = 100
plt.figure(figsize=(30,20))
for batch in test_datagent.flow(x, batch_size=1):
    pred = new_model.predict(batch)
    for j in range(len(batch)):
        plt.subplot(100, 1, i+1)
        plt.title(class_names[np.argmax(pred[j])])
        imgplot = plt.imshow(batch[0])
        plt.imsave(('/path/to/output/ {}.jpg'.format(
        class_names[np.argmax(pred[j])] + str(i))), batch[j])

        i -=1
        if i == -1:
             break

然后您只需要编写值:

var valueDto = (DateTimeOffset)(DateTime)value;
var milliseconds = (valueDto).ToUnixTimeMilliseconds();

结合起来,我们得到:

writer.WriteValue(milliseconds);