Skip to content
Permalink
Browse files

JSONパースの実装とテスト

  • Loading branch information...
yukihane committed Jul 5, 2015
1 parent e63eb3f commit 6b88bfc4016db5cf7a4a6d41e4bee72656e9cb97
@@ -0,0 +1,79 @@
package yukihane.dq10don.utils;

/**
* Created by yuki on 15/07/05.
*/
public class CharacterDto {
private String characterName;
private String iconUrl;
private String job;
private int jobId;
private int lv;
private int slotNo;
private String smileUniqueNo;
private long webPcNo;

public String getCharacterName() {
return characterName;
}

public void setCharacterName(String characterName) {
this.characterName = characterName;
}

public String getIconUrl() {
return iconUrl;
}

public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

public int getJobId() {
return jobId;
}

public void setJobId(int jobId) {
this.jobId = jobId;
}

public int getLv() {
return lv;
}

public void setLv(int lv) {
this.lv = lv;
}

public int getSlotNo() {
return slotNo;
}

public void setSlotNo(int slotNo) {
this.slotNo = slotNo;
}

public String getSmileUniqueNo() {
return smileUniqueNo;
}

public void setSmileUniqueNo(String smileUniqueNo) {
this.smileUniqueNo = smileUniqueNo;
}

public long getWebPcNo() {
return webPcNo;
}

public void setWebPcNo(long webPcNo) {
this.webPcNo = webPcNo;
}
}
@@ -1,7 +1,67 @@
package yukihane.dq10don.utils;

import java.util.ArrayList;
import java.util.List;

/**
* Created by yuki on 15/07/05.
*/
public class LoginAccountDto {
private int accountType;

private List<CharacterDto> characterList = new ArrayList<>();

private String cisuserid;
private int resultCode;
private String sessionId;
private int slotSize;

public int getAccountType() {
return accountType;
}

public void setAccountType(int accountType) {
this.accountType = accountType;
}

public List<CharacterDto> getCharacterList() {
return characterList;
}

public void setCharacterList(List<CharacterDto> characterList) {
this.characterList = characterList;
}


public String getCisuserid() {
return cisuserid;
}

public void setCisuserid(String cisuserid) {
this.cisuserid = cisuserid;
}

public int getResultCode() {
return resultCode;
}

public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}

public String getSessionId() {
return sessionId;
}

public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}

public int getSlotSize() {
return slotSize;
}

public void setSlotSize(int slotSize) {
this.slotSize = slotSize;
}
}
@@ -5,28 +5,32 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginJsonParse {

private static final String TAG = "LoginJsonParse";
private static final Pattern PATTERN = Pattern.compile("\\{.+\\}");

private final Logger logger = LoggerFactory.getLogger(LoginJsonParse.class);
private LoginAccountDto result;

@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String json) {
public void processHTML(String html) {
try {
Matcher matcher = PATTERN.matcher(json);
Matcher matcher = PATTERN.matcher(html);
if (matcher.find()) {
String res = matcher.group();
String jsonText = matcher.group();
ObjectMapper mapper = new ObjectMapper();
LoginAccountDto dto = mapper.readValue(json, LoginAccountDto.class);
result = mapper.readValue(jsonText, LoginAccountDto.class);
}
} catch (IOException e) {
Log.e("exception", "JSON parse failed.", e);
logger.error("parse error", e);
}
}

@@ -0,0 +1,48 @@
package yukihane.dq10don.utils;

import static org.junit.Assert.*;
import android.annotation.TargetApi;
import android.os.Build;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
* Created by yuki on 15/07/05.
*/
public class LoginJsonParseTest {

private static String json;

@TargetApi(Build.VERSION_CODES.KITKAT)
@BeforeClass
public static void beforeClass() throws IOException, URISyntaxException {

URI file = LoginJsonParseTest.class.getClassLoader().getResource("oauth.json").toURI();

try (FileReader reader = new FileReader(new File(file))) {
BufferedReader br = new BufferedReader(reader);
json = br.readLine();
}
}

@Test
public void testProcessHTML() throws Exception {
LoginJsonParse parse = new LoginJsonParse();
parse.processHTML(json);
LoginAccountDto result = parse.getResult();

assertEquals(1, result.getAccountType());
}
}
@@ -0,0 +1 @@
{"accountType":1,"characterList":[{"characterName":"キャラ名1","iconUrl":"http://faceicon.dqx.jp/icon1/hoge.png","job":"レンジャー","jobId":11,"lv":85,"slotNo":1,"smileUniqueNo":"XX999-999","webPcNo":99999999},{"characterName":"キャラ2","iconUrl":"http://faceicon.dqx.jp/icon2/chara2.png","job":"僧侶","jobId":3,"lv":56,"slotNo":2,"smileUniqueNo":"YY888-888","webPcNo":8888888},{"characterName":"キャラ3","iconUrl":"http://faceicon.dqx.jp/icon2/chara3.png","job":"魔法使い","jobId":4,"lv":75,"slotNo":3,"smileUniqueNo":"ZZ777-777","webPcNo":6666666}],"cisuserid":"dummy_cisuserid","resultCode":0,"sessionId":"sessionid_dummy","slotSize":3}

0 comments on commit 6b88bfc

Please sign in to comment.
You can’t perform that action at this time.