11package wu .seal .jsontokotlin ;
22
3- import com .google .gson .Gson ;
4- import com .google .gson .JsonElement ;
5- import com .google .gson .JsonObject ;
6- import com .google .gson .JsonSyntaxException ;
3+ import com .google .gson .*;
74import com .intellij .openapi .actionSystem .AnAction ;
85import com .intellij .openapi .actionSystem .AnActionEvent ;
96import com .intellij .openapi .actionSystem .PlatformDataKeys ;
2522import javax .swing .text .JTextComponent ;
2623import java .awt .*;
2724import java .awt .event .ActionEvent ;
25+ import java .util .IllegalFormatFlagsException ;
2826
2927/**
3028 * Created by Seal.Wu on 2017/8/18.
@@ -54,13 +52,11 @@ public void actionPerformed(AnActionEvent event) {
5452 }
5553 final Messages .InputDialog inputDialog = new Messages .InputDialog (project , "Please input the Json Data" , "Input Json"
5654 , Messages .getInformationIcon (), "" , new InputValidator () {
57- private final Gson gson = new Gson ();
58-
5955 @ Override
6056 public boolean checkInput (String inputString ) {
6157 try {
62- JsonElement jsonElement = gson . fromJson (inputString , JsonObject . class );
63- return true ;
58+ JsonElement jsonElement = new JsonParser (). parse (inputString );
59+ return jsonElement . isJsonObject () || jsonElement . isJsonArray () ;
6460 } catch (JsonSyntaxException e ) {
6561 return false ;
6662 }
@@ -123,8 +119,14 @@ protected JComponent createScrollableTextComponent() {
123119 final Document document = editor .getDocument ();
124120 ImportClassWriter .INSTANCE .insertImportClassCode (project , document );
125121
126- final KotlinMaker maker = new KotlinMaker (className , jsonString );
127- final VirtualFile virtualFile = FileDocumentManager .getInstance ().getFile (document );
122+ final KotlinMaker maker ;
123+ try {
124+ maker = new KotlinMaker (className , jsonString );
125+ } catch (IllegalFormatFlagsException e ) {
126+ e .printStackTrace ();
127+ Messages .showErrorDialog (e .getMessage (), "Unsupport Json" );
128+ return ;
129+ }
128130
129131 CommandProcessor .getInstance ().executeCommand (project , new Runnable () {
130132 @ Override
@@ -144,7 +146,7 @@ public void run() {
144146 } else {
145147 offset = document .getTextLength () - 1 ;
146148 }
147- document .insertString (offset , maker .makeKotlinData ());
149+ document .insertString (Math . max ( offset , 0 ) , maker .makeKotlinData ());
148150
149151 }
150152 });
0 commit comments