Skip to content

Commit 29b421d

Browse files
committed
change double value for gson number deserializer, and change exception for file uploading while local cache is disabled.
1 parent d9e1c84 commit 29b421d

File tree

7 files changed

+36
-3
lines changed

7 files changed

+36
-3
lines changed

core/src/main/java/cn/leancloud/LCFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ public LCFile apply(@NonNull FileUploadToken fileUploadToken) throws Exception {
568568
try {
569569
PaasClient.getStorageClient().fileCallback(asAuthenticatedUser, completeResult);
570570
if (null != exception) {
571-
logger.w("failed to invoke fileCallback. cause:", exception);
571+
logger.w("failed to upload file. cause: " + exception.getMessage());
572572
throw exception;
573573
} else {
574574
return LCFile.this;
@@ -720,7 +720,7 @@ public InputStream getDataStream() throws Exception {
720720
filePath = localPath;
721721
} else if (!StringUtil.isEmpty(cachePath)) {
722722
filePath = cachePath;
723-
} else if (!StringUtil.isEmpty(getUrl())) {
723+
} else if (!FileCache.getIntance().isDisableLocalCache() && !StringUtil.isEmpty(getUrl())) {
724724
File cacheFile = FileCache.getIntance().getCacheFile(getUrl());
725725
if (null == cacheFile || !cacheFile.exists()) {
726726
FileDownloader downloader = new FileDownloader();

core/src/main/java/cn/leancloud/cache/FileCache.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public String saveLocalFile(String name, File localFile) {
2626

2727
@Override
2828
public File getCacheFile(String url) {
29+
if (isDisableLocalCache()) {
30+
return null;
31+
}
2932
String urlMd5 = MDFive.computeMD5(url.getBytes());
3033
return super.getCacheFile(urlMd5);
3134
}

core/src/main/java/cn/leancloud/cache/LocalStorage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public File getCacheFile(String key) {
8686
return new File(this.baseDir + key);
8787
}
8888

89+
public boolean isDisableLocalCache() {
90+
return disableLocalCache;
91+
}
8992
public InputStream getInputStreamFromFile(File file) throws FileNotFoundException {
9093
if (disableLocalCache) {
9194
return null;

core/src/main/java/cn/leancloud/gson/NumberDeserializerDoubleAsIntFix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Object parsePrecisionNumber(Number num) {
1818
// and return any type you want
1919
// this solution will transform 3.0 float to long values
2020
if (Math.floor(num.doubleValue()) != Math.ceil(num.doubleValue())) {
21-
return num;
21+
return num.doubleValue();
2222
}
2323
double doubleValue = Math.ceil(num.doubleValue());
2424
if (doubleValue == num.intValue()) {

core/src/main/java/cn/leancloud/upload/QiniuSlicingUploader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public void onProgress(int progress) {
5454

5555
try {
5656
is = this.avFile.getDataStream();
57+
if (null == is) {
58+
return new LCException(LCException.FILE_UPLOAD_FAILURE,
59+
"failed to upload file to qiniu bcz current file has not data stream.");
60+
}
5761
LOGGER.d("begin to upload qiniu. chunkSize=" + uploadChunkSize + ", blockCount=" + blockCount + ", is=" + is);
5862
// loop for read, upload block to qiniu.
5963
for (int i = 0; i< blockCount; i++) {

core/src/test/java/cn/leancloud/LCFileTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.leancloud;
22

3+
import cn.leancloud.core.AppConfiguration;
34
import cn.leancloud.core.LeanCloud;
45
import cn.leancloud.types.LCNull;
56
import cn.leancloud.utils.StringUtil;
@@ -172,6 +173,7 @@ public void onComplete() {
172173
}
173174

174175
public void testBase64Data() throws Exception {
176+
AppConfiguration.setEnableLocalCache(false);
175177
String contents = StringUtil.getRandomString(640);
176178
LCFile file = new LCFile("testfilename", contents.getBytes());
177179
// Map<String, Object> metaData = new HashMap<>();

core/src/test/java/cn/leancloud/json/GsonCommonTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cn.leancloud.sms.LCCaptchaDigest;
1010
import com.google.gson.Gson;
1111
import com.google.gson.GsonBuilder;
12+
import com.google.gson.JsonElement;
1213
import com.google.gson.reflect.TypeToken;
1314
import junit.framework.TestCase;
1415

@@ -149,6 +150,26 @@ public void testDoubleAndLong() {
149150
System.out.println(responses.toString());
150151
}
151152

153+
public void testObjectDeserialization() {
154+
String input = "{\"result\":{\"objectId\":\"637233023f103a0cf936caf8\",\"billId\":\"202211142022269827\"," +
155+
"\"state\":-1,\"size\":5,\"price\":10.6,\"payPrice\":0,\"isPaid\":false," +
156+
"\"createdAt\":\"2022-11-14T12:22:26.122Z\",\"isCommented\":false,\"purpose\":\"用于小说配音\"," +
157+
"\"user\":{\"objectId\":\"5e5462617796d9006a09c795\",\"name\":\"方冶(全天接单主页加微)\"," +
158+
"\"iconUrl\":\"http://file2.i7play.com/j6ptbHjPSAaBurvOYMPRgevtTzbwXYNF/CROP_20221112164622689.jpg\"," +
159+
"\"id\":2230377,\"wx\":\"ZmZxy-18\",\"qq\":\"\",\"isVip\":true,\"is18\":true},\"isBu\":false}} ";
160+
Type ResponseMap = new TypeToken<Map<String, Object>>() {}.getType();
161+
Map<String, Object> result = GsonWrapper.getGsonInstance().fromJson(input, ResponseMap);
162+
Object data = result.get("result");
163+
JsonElement jsonElement = GsonWrapper.getGsonInstance().toJsonTree(data);
164+
System.out.println(jsonElement);
165+
if (data instanceof Map) {
166+
Map<String,Object> dataMap = (Map<String,Object>)data;
167+
Object priceObj = dataMap.get("price");
168+
System.out.println(priceObj);
169+
}
170+
System.out.println(result.toString());
171+
}
172+
152173
public void testNumberParser() {
153174
Number numbers[] = {3, 4.5, 5.0, -0, 0.0, 0.0002, -0.0002, -5, -6.5};
154175
for (Number num: numbers) {

0 commit comments

Comments
 (0)