Json, gwt and reflection

Anything libgdx related goes here!

Json, gwt and reflection

Postby Torleif » Thu Jun 15, 2017 8:28 pm

Hi

I'm having trouble with json.tojson on gwt. It works on desktop and android.
I've tried lots of different ways to make it work, but I always get an error message.

I get the error when calling,
Code: Select all
public void saveBoard(SavedBoard thisBoard) {
      boardToString = json.toJson(thisBoard, ClassReflection.forName(SavedBoard.class.getName()));
......

The SaveBoard class have the following members with empty constructor, and below them are the classes referenced by SaveBoard


SAVEBOARD:
Code: Select all
public class SavedBoard {

   public Array<IntArray> tileSkeleton;
   private int nextType;
   private int height;
   private int width;
   private ScoreSheet scoreSheet;
   private boolean isCompleted;
   private BoardRules boardRules;
   
   public SavedBoard() {
      try {
         tileSkeleton = (Array<IntArray>) ClassReflection.newInstance(Array.class);         
      } catch (ReflectionException e) {
      }
   }
}

SCORESHEET:
Code: Select all
public class ScoreSheet {
   private int bonus = 5;

   public IntMap<Score> turnScores;
   private int totalScore = 0;

   public ScoreSheet() {
      try {
         turnScores = (IntMap<Score>) ClassReflection.newInstance(ClassReflection.forName("com.badlogic.gdx.utils.IntMap"));         
      } catch (ReflectionException e) {

      }
   }
}

SCORE:
Code: Select all
public class Score {
   private int basic =0;
   private int extraTilesBonus = 0;
   private int sequelBonus = 0;
   private int combinationBonus = 0;
   private int noInSequel = 0;
   
   public Score() {
      
   }
}


BOARDRULES:
Code: Select all
public class BoardRules {
   private int startColors;
   private int colors;
   private int equals;
   protected final RandomXS128 random;
   private int tilesDrawn = 0;
   private float a = 0.18f;
   private float b = 0.4f;
   private float c = 0.4f;
   
   public BoardRules() {
      random = new RandomXS128();      
      
   }
}


In the gwt.xml file under core and GdxDefinition.gwt.xml under html project I've added the following lines:
Code: Select all
   <extend-configuration-property name="gdx.reflect.include" value='no.devlye.swipematch.logic.board.SavedBoard' />
   <extend-configuration-property name="gdx.reflect.include" value='no.devlye.swipematch.logic.ScoreSheet' />
   <extend-configuration-property name="gdx.reflect.include" value='no.devlye.swipematch.logic.Score' />
   <extend-configuration-property name="gdx.reflect.include" value='com.badlogic.gdx.utils.IntArray' />
   <extend-configuration-property name="gdx.reflect.include" value='com.badlogic.gdx.utils.IntMap' />
   <extend-configuration-property name="gdx.reflect.include" value='no.devlye.swipematch.logic.BoardRules' />
   <extend-configuration-property name="gdx.reflect.include" value='com.badlogic.gdx.math.RandomXS128' />
   <extend-configuration-property name="gdx.reflect.include" value='com.badlogic.gdx.utils.Array' />
   <extend-configuration-property name="gdx.reflect.include" value='java.util.Random' />

I've basically just added more lines when I've gotten new errors in gwt.

When I run as superDev in the browser , the game starts to run but crashes on the following line:
Code: Select all
SaveBoard thisBoard
boardToString = json.toJson(thisBoard, ClassReflection.forName(SavedBoard.class.getName()));



With this setup I get the following error
Code: Select all
html-0.js:70943 Uncaught Error: java.lang.RuntimeException: com.badlogic.gdx.utils.SerializationException: Serialization trace: com.badlogic.gdx.utils.reflect.Field@46 (no.devlye.swipematch.logic.BoardRules) com.badlogic.gdx.utils.reflect.Field@47 (no.devlye.swipematch.logic.board.SavedBoard)
    at yOc_g$.QNc_g$ [as createError_0_g$] (Throwable.java:121)
    at yOc_g$.$Nc_g$ [as initializeBackingError_0_g$] (Throwable.java:113)
    at yOc_g$.MNc_g$ (Throwable.java:81)
    at yOc_g$.qOc_g$ (Exception.java:37)
    at yOc_g$ (RuntimeException.java:37)
    at uj_g$.vj_g$ [as execute_0_g$] (GwtApplication.java:221)
    at AnimationSchedulerImplStandard.java:39
    at Ckg_g$ (Impl.java:239)
    at Fkg_g$ (Impl.java:298)
    at Impl.java:77


Can anyone help me? There is something with reflection that I'm not understanding.
Torleif
 
Posts: 3
Joined: Sat Sep 26, 2015 8:21 pm

Re: Json, gwt and reflection

Postby evilentity » Thu Jun 15, 2017 9:58 pm

Whats up with all this?
Code: Select all
try {
         turnScores = (IntMap<Score>) ClassReflection.newInstance(ClassReflection.forName("com.badlogic.gdx.utils.IntMap"));         
      } catch (ReflectionException e) {

      }


Gwt is pain. Test with a simpler case to make sure you understand where the problem is. Simple class with few primitive fields would be a good start.
Looking for a freelancer? PM me!
evilentity
 
Posts: 3751
Joined: Wed Aug 24, 2011 11:37 am

Re: Json, gwt and reflection

Postby AngelusWeb » Sun Jun 18, 2017 6:22 pm

I concur that GWT is a pain. I haven't yet been able to get it to work (on anything more complex than a demo).

Even the congnoscenti (those "in the know") at LibGDX can't tell you which library functions will disqualify you from being able to compile to the GWT (web) platform. Even if you look at the official docs (which are perhaps a start -- they tell you which things aren't available in GWT) you might still have blockers preventing you from a successful GWT deployment.

Here is an existing thread on the topic:
viewtopic.php?f=11&t=22294
AngelusWeb
 
Posts: 402
Joined: Fri Jul 20, 2012 8:19 pm

Re: Json, gwt and reflection

Postby MrStahlfelge » Mon Jun 19, 2017 12:17 pm

GWT is sometimes a pain, but it also is a really great feature of libGDX.

I have to problems with gdx-gwt:

1. Documentation of libGDX-gwt is not good, as you say. You see what is working when it is not.
In this case, the documentation lacks two things: It is documented very well how you can read and write Json objects with the reflection. However, this brings you in trouble with GWT and also with ProGuard. So in most cases, why don't read Json values with JsonReader.parse() and going through the JsonValue objects? That is no problem at all on any platform and with obfusciation.

2. GWT compile seems to be unimportant for the ones leading the project.
I really appreciate the work of the ones that made libGDX for us. And it must have been a very big effort to get the whole GWT integration working. That is why I really don't understand why they don't treat it very well. If the GWT backend was my baby, I would promote and maintain it far more. Whole 1.9.5 GWT is not useable, BitmapFonts are not working. Which game does not use BitmapFont? The controller extension is not working. That is a minor issue, okay. But it is not working for 3 years and the pull requests to get it fixed aren't pulled into the repository.

To repeat it clear: I really appreciate the work that is done for us and I don't want to offend someone. I just wanted to express that there are things I don't understand. ;)
MrStahlfelge
 
Posts: 27
Joined: Thu Jun 15, 2017 6:40 am

Re: Json, gwt and reflection

Postby AngelusWeb » Sat Jun 24, 2017 8:52 pm

MrStahlfelge wrote:GWT is sometimes a pain, but it also is a really great feature of libGDX.

I have to problems with gdx-gwt:

1. Documentation of libGDX-gwt is not good, as you say. You see what is working when it is not.
In this case, the documentation lacks two things: It is documented very well how you can read and write Json objects with the reflection. However, this brings you in trouble with GWT and also with ProGuard. So in most cases, why don't read Json values with JsonReader.parse() and going through the JsonValue objects? That is no problem at all on any platform and with obfusciation.

2. GWT compile seems to be unimportant for the ones leading the project.
Whole 1.9.5 GWT is not useable, BitmapFonts are not working. Which game does not use BitmapFont? The controller extension is not working. That is a minor issue, okay. But it is not working for 3 years and the pull requests to get it fixed aren't pulled into the repository.


It sounds worse than I thought. BitmapFont isn't working in GWT? You're right, every non-trivial game needs to have fonts of some kind. And they aren't bothering to pull requests that would fix GWT?

That sounds quite serious, from the standpoint of LibGDX support, viability, feasibility, etc.

I've invested a lot of time learning LibGDX and I'd hate to have to give up on it. That's why I'm loathe to do so.
Plus I have 2 1/2 complete games written in LibGDX, which I don't exactly want to abandon.

But Phaser.io is also open source, works for us Linux users, targets the browser (which covers "desktop" Windows/Linux/Mac quite nicely) and mobile devices, and they have a vibrant community, lots of great tutorials, and updated releases every month or so. It's very tempting.
AngelusWeb
 
Posts: 402
Joined: Fri Jul 20, 2012 8:19 pm

Re: Json, gwt and reflection

Postby MrStahlfelge » Sun Jun 25, 2017 9:16 am

To make this clear, BitmapFonts did not work in GWT in libGDX 1.9.5. You can use 1.9.4 or 1.9.6 and it works. But apparently no one noticed before releasing 1.9.5 - this leads me to the assumption that GWT is not used by the libGDX responsibles themselves.

As for the pull requests to fix the controllers. This one is from April and not pulled. I fixed the issue myself, not realizing that there is already a pull request for it, requested pull two weeks ago. Was then pointed to the existing fix by another user.
MrStahlfelge
 
Posts: 27
Joined: Thu Jun 15, 2017 6:40 am

Re: Json, gwt and reflection

Postby AngelusWeb » Sun Jun 25, 2017 5:10 pm

MrStahlfelge wrote:To make this clear, BitmapFonts did not work in GWT in libGDX 1.9.5. You can use 1.9.4 or 1.9.6 and it works. But apparently no one noticed before releasing 1.9.5 - this leads me to the assumption that GWT is not used by the libGDX responsibles themselves.

As for the pull requests to fix the controllers. This one is from April and not pulled. I fixed the issue myself, not realizing that there is already a pull request for it, requested pull two weeks ago. Was then pointed to the existing fix by another user.


Well that's a little better. I think there's still hope for the platform. It's been around for many years, and it seems to be the best library for Android games that I've seen (or at least in the top 2). Especially the fact that it doesn't require Windows, Mac, or a % of your revenue to use it!

I just wish someone would rise up and really take the reins on this thing. LibGDX seems to be "past history" for the original creator, Mario. It wouldn't take much to get the library's act together and really really push it forward (new documentation, new tutorials, fix up GWT support, etc.) but if there's one serious weakness to libGDX, it is: lack of leadership/direction. There's a real vacuum there.

Let's put it this way: it's not inherent to open source projects, or free software, to languish like LibGDX has. Phaser.io and a thousand other open-source, free projects are plenty "alive". But they seem to have something that LibGDX currently lacks. I'm just being honest here, because I like LibGDX and I can tell clear as day what its Achilles Heel is.
AngelusWeb
 
Posts: 402
Joined: Fri Jul 20, 2012 8:19 pm


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot] and 12 guests