簡體   English   中英

Avalara:json 日期的“DateTime”有效格式是什么?

[英]Avalara: What is a “DateTime” valid format for the json date?

Avalara 的正確 JSON 日期格式是什么? 以下代碼:

 TransactionModel transaction = new TransactionBuilder(client, "COMPANY", DocumentType.SalesOrder, "myCompany.")
    .withDate(Calendar.getInstance().getTime())
    .withAddress(TransactionAddressType.SingleLocation, null, null, null, null, null, zipCode, "US")
    .withLine( new BigDecimal(100.0), new BigDecimal(1), "P0000000")
    .Create();

引發不指示正確格式的異常:

com.google.gson.JsonSyntaxException: 2019-10-01
    at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
    at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
    at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
    at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at com.google.gson.Gson.fromJson(Gson.java:717)
    at net.avalara.avatax.rest.client.serializer.JsonSerializer.DeserializeObject(JsonSerializer.java:15)
    at net.avalara.avatax.rest.client.RestCall.call(RestCall.java:99)
    at net.avalara.avatax.rest.client.AvaTaxClient.createTransaction(AvaTaxClient.java:19174)
    at net.avalara.avatax.rest.client.TransactionBuilder.Create(TransactionBuilder.java:425)

如有疑問,請閱讀來源。 看起來com.google.gson.DefaultDateTypeAdapter有幾種默認日期格式,它將嘗試在deserializeToDate中使用。 因此,請確保您使用其中之一。

大多數日期格式來自java.text.DateFormat

還要檢查AvaTax-REST-V2的來源

如果您在編輯器中鏈接了源代碼,那么我建議在堆棧跟蹤中的幾個位置放置一個斷點以查看發生了什么。 一個好的候選人當然是deserializeToDate化ToDate。


DefaultDateTypeAdapter.java

/**
 * List of 1 or more different date formats used for de-serialization attempts. The first of them is
 * used for serialization as well.
 */
private final List<DateFormat> dateFormats = new ArrayList<DateFormat>();

DefaultDateTypeAdapter(Class<? extends Date> dateType) {
    this.dateType = verifyDateType(dateType);
    dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US));
    if (!Locale.getDefault().equals(Locale.US)) {
        dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT));
    }
    if (JavaVersion.isJava9OrLater()) {
        dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(DateFormat.DEFAULT, DateFormat.DEFAULT));
    }
}

DefaultDateTypeAdapter(Class<? extends Date> dateType, String datePattern) {
    this.dateType = verifyDateType(dateType);
    dateFormats.add(new SimpleDateFormat(datePattern, Locale.US));
    if (!Locale.getDefault().equals(Locale.US)) {
        dateFormats.add(new SimpleDateFormat(datePattern));
    }
}

DefaultDateTypeAdapter(Class<? extends Date> dateType, int style) {
    this.dateType = verifyDateType(dateType);
    dateFormats.add(DateFormat.getDateInstance(style, Locale.US));
    if (!Locale.getDefault().equals(Locale.US)) {
        dateFormats.add(DateFormat.getDateInstance(style));
    }
    if (JavaVersion.isJava9OrLater()) {
        dateFormats.add(PreJava9DateFormatProvider.getUSDateFormat(style));
    }
}

public DefaultDateTypeAdapter(int dateStyle, int timeStyle) {
    this(Date.class, dateStyle, timeStyle);
}

public DefaultDateTypeAdapter(Class<? extends Date> dateType, int dateStyle, int timeStyle) {
    this.dateType = verifyDateType(dateType);
    dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US));
    if (!Locale.getDefault().equals(Locale.US)) {
        dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle));
    }
    if (JavaVersion.isJava9OrLater()) {
        dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(dateStyle, timeStyle));
    }
}

對於在這里找到自己方式的任何其他人,可以在 Avalara 開發人員論壇上獲得更完整的討論: https://community.avalara.com/avalara/topics/error-parsing-date-jre-sdk

簡短回答:升級您對 gson 的依賴,您的代碼沒有問題。 我移到了更新的版本並修復了錯誤:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM