mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-04-19 07:15:54 -04:00
Merge e290fd20fcd3e5ce4efc7b7b66fa192f6c9e9db3 into 28d2bc891f6764f239c8a16d0ada68ff9c7521a3
This commit is contained in:
commit
3a31a5a5f1
@ -71,7 +71,6 @@ import javax.annotation.Nullable;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@ -341,12 +340,10 @@ public abstract class PaymentAccount implements PersistablePayload {
|
||||
// ---------------------------- SERIALIZATION -----------------------------
|
||||
|
||||
public String toJson() {
|
||||
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
||||
if (paymentAccountPayload != null) jsonMap.putAll(gsonBuilder.create().fromJson(paymentAccountPayload.toJson(), (Type) Object.class));
|
||||
jsonMap.put("accountName", getAccountName());
|
||||
jsonMap.put("accountId", getId());
|
||||
if (paymentAccountPayload != null) jsonMap.put("salt", getSaltAsHex());
|
||||
return gsonBuilder.create().toJson(jsonMap);
|
||||
Gson gson = gsonBuilder
|
||||
.registerTypeAdapter(PaymentAccount.class, new PaymentAccountTypeAdapter(this.getClass()))
|
||||
.create();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -388,12 +385,7 @@ public abstract class PaymentAccount implements PersistablePayload {
|
||||
PaymentAccountForm form = new PaymentAccountForm(PaymentAccountForm.FormId.valueOf(paymentMethod.getId()));
|
||||
for (PaymentAccountFormField.FieldId fieldId : getInputFieldIds()) {
|
||||
PaymentAccountFormField field = getEmptyFormField(fieldId);
|
||||
Object value = jsonMap.get(HavenoUtils.toCamelCase(field.getId().toString()));
|
||||
if (value instanceof List) { // TODO: list should already be serialized to comma delimited string in PaymentAccount.toJson() (PaymentAccountTypeAdapter?)
|
||||
field.setValue(String.join(",", (List<String>) value));
|
||||
} else {
|
||||
field.setValue((String) value);
|
||||
}
|
||||
field.setValue((String) jsonMap.get(HavenoUtils.toCamelCase(field.getId().toString())));
|
||||
form.getFields().add(field);
|
||||
}
|
||||
return form;
|
||||
|
@ -40,6 +40,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static haveno.common.util.ReflectionUtils.getSetterMethodForFieldInClassHierarchy;
|
||||
import static haveno.common.util.ReflectionUtils.getVisibilityModifierAsString;
|
||||
@ -102,6 +103,12 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
// We're not serializing a real payment account instance here.
|
||||
out.beginObject();
|
||||
|
||||
// Write extra fields
|
||||
out.name("accountName");
|
||||
out.value(account.getAccountName());
|
||||
out.name("accountId");
|
||||
out.value(account.getId());
|
||||
|
||||
writeComments(out, account);
|
||||
|
||||
out.name("paymentMethodId");
|
||||
@ -112,7 +119,8 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
|
||||
// The last field in all json forms is the empty, editable salt field.
|
||||
out.name("salt");
|
||||
out.value("");
|
||||
out.value(account.getSaltAsHex());
|
||||
|
||||
|
||||
out.endObject();
|
||||
}
|
||||
@ -135,7 +143,11 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
}
|
||||
|
||||
|
||||
private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) {
|
||||
private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) throws IOException {
|
||||
if (account instanceof CountryBasedPaymentAccount) {
|
||||
writeAcceptedCountryCodesField(out, account);
|
||||
}
|
||||
|
||||
if (account.hasMultipleCurrencies()) {
|
||||
writeTradeCurrenciesField(out, account);
|
||||
writeSelectedTradeCurrencyField(out, account);
|
||||
@ -143,7 +155,6 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
|
||||
fieldSettersMap.forEach((field, value) -> {
|
||||
try {
|
||||
// Write out a json element if there is a @Setter for this field.
|
||||
if (value.isPresent()) {
|
||||
log.debug("Append form with settable field: {} {} {} setter: {}",
|
||||
getVisibilityModifierAsString(field),
|
||||
@ -152,14 +163,13 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
value);
|
||||
String fieldName = field.getName();
|
||||
out.name(fieldName);
|
||||
if (fieldName.equals("country")) out.value("your two letter country code");
|
||||
else out.value("your " + fieldName.toLowerCase());
|
||||
if (fieldName.equals("country"))
|
||||
out.value("your two letter country code");
|
||||
else
|
||||
out.value("your " + fieldName.toLowerCase());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
String errMsg = format("cannot create a new %s json form",
|
||||
account.getClass().getSimpleName());
|
||||
log.error(capitalize(errMsg) + ".", ex);
|
||||
throw new IllegalStateException("programmer error: " + errMsg);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException("Error writing field " + field.getName(), ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -168,18 +178,16 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
// field in the json form, though the 'tradeCurrencies' field has no setter method in
|
||||
// the PaymentAccount class hierarchy. At of time of this change, TransferwiseAccount
|
||||
// is the only known exception to the rule.
|
||||
private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) {
|
||||
try {
|
||||
String fieldName = "tradeCurrencies";
|
||||
log.debug("Append form with non-settable field: {}", fieldName);
|
||||
out.name(fieldName);
|
||||
out.value("comma delimited currency code list, e.g., gbp,eur,jpy,usd");
|
||||
} catch (Exception ex) {
|
||||
String errMsg = format("cannot create a new %s json form",
|
||||
account.getClass().getSimpleName());
|
||||
log.error(capitalize(errMsg) + ".", ex);
|
||||
throw new IllegalStateException("programmer error: " + errMsg);
|
||||
private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) throws IOException {
|
||||
out.name("tradeCurrencies");
|
||||
List<TradeCurrency> tradeCurrencies = account.getTradeCurrencies();
|
||||
String tradeCurrenciesValue = "";
|
||||
if (tradeCurrencies != null && !tradeCurrencies.isEmpty()) {
|
||||
tradeCurrenciesValue = tradeCurrencies.stream()
|
||||
.map(TradeCurrency::getCode)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
out.value(tradeCurrenciesValue);
|
||||
}
|
||||
|
||||
// PaymentAccounts that support multiple 'tradeCurrencies' need to define a
|
||||
@ -199,6 +207,23 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
|
||||
}
|
||||
}
|
||||
|
||||
private void writeAcceptedCountryCodesField(JsonWriter out, PaymentAccount account) throws IOException {
|
||||
out.name("acceptedCountryCodes");
|
||||
if (account instanceof CountryBasedPaymentAccount) {
|
||||
List<Country> acceptedCountries = ((CountryBasedPaymentAccount) account).getAcceptedCountries();
|
||||
String countryCodesValue = "";
|
||||
if (acceptedCountries != null && !acceptedCountries.isEmpty()) {
|
||||
countryCodesValue = acceptedCountries.stream()
|
||||
.map(c -> c.code) // assuming 'code' is accessible or use getCode()
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
out.value(countryCodesValue);
|
||||
} else {
|
||||
out.value("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PaymentAccount read(JsonReader in) throws IOException {
|
||||
PaymentAccount account = initNewPaymentAccount();
|
||||
|
Loading…
x
Reference in New Issue
Block a user