You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.8 KiB
111 lines
3.8 KiB
package com.gyf.csams.util
|
|
|
|
import com.google.gson.*
|
|
import com.google.gson.reflect.TypeToken
|
|
import com.gyf.csams.association.model.*
|
|
import com.gyf.csams.uikit.StringForm
|
|
import java.lang.reflect.Type
|
|
|
|
class OpenQuestionsVoSerializer: JsonSerializer<OpenQuestionsVo> {
|
|
override fun serialize(
|
|
vo: OpenQuestionsVo?,
|
|
p1: Type?,
|
|
p2: JsonSerializationContext?
|
|
): JsonElement {
|
|
val gson= Gson()
|
|
val c = JsonObject()
|
|
c.add("examType", gson.toJsonTree(vo?.examType?.name))
|
|
c.add("question",gson.toJsonTree(vo?.question?.formValue?.value))
|
|
return c
|
|
}
|
|
}
|
|
|
|
class OpenQuestionsVoDeserializer: JsonDeserializer<OpenQuestionsVo> {
|
|
override fun deserialize(
|
|
json: JsonElement?,
|
|
p1: Type?,
|
|
p2: JsonDeserializationContext?
|
|
): OpenQuestionsVo {
|
|
val root=json?.asJsonObject
|
|
val value=root?.get("question")?.asString
|
|
val question= StringForm(formDesc = "问题",textLength = QUESTION_TEXT_LENGTH)
|
|
if (value != null) {
|
|
question.onChange(value)
|
|
return OpenQuestionsVo(question = question)
|
|
}else{
|
|
throw NullPointerException("问题无法解析!!!!")
|
|
}
|
|
}
|
|
}
|
|
|
|
class ChoiceQuestionVoSerializer: JsonSerializer<ChoiceQuestionVo> {
|
|
override fun serialize(
|
|
vo: ChoiceQuestionVo?,
|
|
p1: Type?,
|
|
p2: JsonSerializationContext?
|
|
): JsonElement {
|
|
val gson= Gson()
|
|
val c = JsonObject()
|
|
c.add("examType", gson.toJsonTree(vo?.examType?.name))
|
|
c.add("question",gson.toJsonTree(vo?.question?.formValue?.value))
|
|
c.add("answers",gson.toJsonTree(vo?.answers))
|
|
c.add("rightAnswer",gson.toJsonTree(vo?.rightAnswer))
|
|
return c
|
|
}
|
|
}
|
|
|
|
class ChoiceQuestionVoDeserializer: JsonDeserializer<ChoiceQuestionVo> {
|
|
override fun deserialize(
|
|
json: JsonElement?,
|
|
p1: Type?,
|
|
p2: JsonDeserializationContext?
|
|
): ChoiceQuestionVo {
|
|
val root=json?.asJsonObject
|
|
val value=root?.get("question")?.asString
|
|
val question=StringForm(formDesc = "问题",textLength = QUESTION_TEXT_LENGTH)
|
|
if (value != null) {
|
|
question.onChange(value)
|
|
val gson=Gson()
|
|
val answers:List<String> = gson.fromJson(root.get("answers"),object : TypeToken<List<String>>() {}.type)
|
|
if(answers.size!= ANSWER_SIZE){
|
|
throw IllegalArgumentException("选项数量!=$QUESTION_TEXT_LENGTH")
|
|
}
|
|
val rightAnswer=root.get("rightAnswer").asInt
|
|
return ChoiceQuestionVo(
|
|
question = question,
|
|
answers = answers.map { StringForm(formDesc = "选项",textLength = ANSWER_TEXT_LENGTH,value=it) },
|
|
rightAnswer = rightAnswer
|
|
)
|
|
}else{
|
|
throw NullPointerException("问题无法解析!!!!")
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 题目反序列化
|
|
*
|
|
*/
|
|
class ExamDeserializer:JsonDeserializer<Exam>{
|
|
companion object{
|
|
val gson: Gson =GsonBuilder()
|
|
.registerTypeAdapter(OpenQuestionsVo::class.java,OpenQuestionsVoDeserializer())
|
|
.registerTypeAdapter(ChoiceQuestionVo::class.java,ChoiceQuestionVoDeserializer())
|
|
.create()
|
|
}
|
|
|
|
override fun deserialize(json: JsonElement?, p1: Type?, p2: JsonDeserializationContext?): Exam {
|
|
val root=json?.asJsonObject
|
|
|
|
val type=root?.get("examType")?.asString
|
|
|
|
return when (gson.fromJson(type, ExamType::class.java)) {
|
|
ExamType.cq -> gson.fromJson(json,
|
|
object : TypeToken<ChoiceQuestionVo>() {}.type)
|
|
ExamType.oq -> gson.fromJson(json,
|
|
object : TypeToken<OpenQuestionsVo>() {}.type)
|
|
null->throw NullPointerException("无法识别题目类型!")
|
|
}
|
|
|
|
}
|
|
} |