package com.alibaba.fastjson; import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.SerializeWriter; import com.alibaba.fastjson.serializer.SerializerFeature; import java.io.Closeable; import java.io.Flushable; import java.io.IOException; import java.io.Writer; /* loaded from: classes2.dex */ public class JSONWriter implements Closeable, Flushable { private JSONStreamContext context; private JSONSerializer serializer; private SerializeWriter writer; public JSONWriter(Writer writer) { this.writer = new SerializeWriter(writer); this.serializer = new JSONSerializer(this.writer); } public void config(SerializerFeature serializerFeature, boolean z) { this.writer.config(serializerFeature, z); } public void startObject() { if (this.context != null) { beginStructure(); } this.context = new JSONStreamContext(this.context, 1001); this.writer.write(123); } public void endObject() { this.writer.write(125); endStructure(); } public void writeKey(String str) { writeObject(str); } public void writeValue(Object obj) { writeObject(obj); } public void writeObject(String str) { beforeWrite(); this.serializer.write(str); afterWriter(); } public void writeObject(Object obj) { beforeWrite(); this.serializer.write(obj); afterWriter(); } public void startArray() { if (this.context != null) { beginStructure(); } this.context = new JSONStreamContext(this.context, 1004); this.writer.write(91); } private void beginStructure() { int i = this.context.state; switch (this.context.state) { case 1001: case 1004: return; case 1002: this.writer.write(58); return; case 1003: default: throw new JSONException("illegal state : " + i); case 1005: this.writer.write(44); return; } } public void endArray() { this.writer.write(93); endStructure(); } private void endStructure() { JSONStreamContext jSONStreamContext = this.context.parent; this.context = jSONStreamContext; if (jSONStreamContext == null) { return; } int i = jSONStreamContext.state; int i2 = i != 1001 ? i != 1002 ? i != 1004 ? -1 : 1005 : 1003 : 1002; if (i2 != -1) { this.context.state = i2; } } private void beforeWrite() { JSONStreamContext jSONStreamContext = this.context; if (jSONStreamContext == null) { return; } int i = jSONStreamContext.state; if (i == 1002) { this.writer.write(58); } else if (i == 1003) { this.writer.write(44); } else { if (i != 1005) { return; } this.writer.write(44); } } private void afterWriter() { int i; JSONStreamContext jSONStreamContext = this.context; if (jSONStreamContext == null) { return; } switch (jSONStreamContext.state) { case 1001: case 1003: i = 1002; break; case 1002: i = 1003; break; case 1004: i = 1005; break; default: i = -1; break; } if (i != -1) { this.context.state = i; } } @Override // java.io.Flushable public void flush() throws IOException { this.writer.flush(); } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { this.writer.close(); } }