Tag: Java

Gson Inheritance Issues

January 24, 2011 » Geek

So, work has me writing Java for the first time in my life. I needed to handle some data in JSON format, and was using Jackson and it’s ObjectMapper for a while. Fast and a relatively nice interface.

Today I switched to Gson for the even simpler API. It’s slower, but I’m just munging little strings every once in a while, so that’s not a problem.

The one thing I came across today that didn’t go as expected was serialization with inheritance in my objects. Here’s where I started:

I expected to see the JSON representing of each. Much to my surprise, I got this instead:

What the what? Weird. So I thought I’d try adding a constructor and setting it there.

Again, no luck! I got the same output as before. I gave it one last shot.

That time I got it.

Strange. So it seems that when serializing an object, Gson looks for the value of a field in the superclass. However, when you are in the object you are accessing the local field. It seems like a weird scope thing. Interestingly, if you provide a new implementation at the subclass level it will use that one instead.

This seems to be noted in the user guide at “Finer Points with Objects“. I must have just missed it.

Fields corresponding to the outer classes in inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization

Easy enough, though a little annoying.

Further DK421 Development

January 14, 2007 » Geek

After a lengthy break from DK421 development I started up again this evening. My primary purpose tonight was to learn how to program my arduino and to learn the basics of serial communication with javax.comm. I wrote a led blinker that also sent some data over the serial connection. 0 when it turned the light on, 1 when it turned it off. and yes, I know that seems backwards.

After a short battle I hacked up the config and a sample program to print my data. It’s not printing the right values, but it is printing the wrong ones consistently. I think it has to do with my function to convert a byte array to an integer. It also has a small runtime exception that I didn’t work out. Regardless, it’s working. I’ll install Eclipse on this machine and maybe get it working even further soon. Next step is to get some piezo’s and learn to read them.

On that front I was thinking about using the velocity of an impact to handle the volume it plays back. Maybe something of a code, like if a strike is registered with a strength of 100 on pin 0 it could send 0_100 or something. Thats in the future though. I am worried about the fact that there are only 6 analog pins though. That means 6 drums without adding another board.

Back To The Software

November 8, 2006 » Geek

After some fantastic hardware work I’ve started yet another rewrite of the software for DK421. This time I’m rebuilding it to use more graphical options and let you load new clips without restarting the whole thing. I’ve been learning a bit in my Java class about all this GUI stuff, so I thought I’d apply it.

I’m still hoping someday I can add a save/load functionality to bring in a while sound set. Not the biggest deal at this point, but it would be a nice convenience. I’ve also gotten the first test drum pad cut and am going to buy the other pieces tomorrow. I might have a working pad by tomorrow evening, though my late classes might have something to say about that. I’m tempted to bring my controller to school and work on it between classes. Probably won’t though. Anyway, DK421 is in the early stages of version 0.03 now, I think it’s time to make it a better project page.

P.S. I’ve set the size for this project: 12 Controllers. So that comes out to 1 hi-hat and up to 9 other pads (cymbals, snares, whatever). More than enough for almost any drum kit. I think my first hardware version will be snare, hi-hat, kick, crash, ride. I’ll probably add toms later.

DrumKit V0.02 -> DK421 V0.02

October 12, 2006 » Geek

I’ve been working on my DrumKit again, since I don’t get to play with my Nokia 770. I came up with a better name for this revision, DK421. It doesn’t mean anything. Well, it kinda does. It came from a Stormtrooper in ANH (“TK421, Why aren’t you at your post? TK421, respond.”)

There isn’t a lot of change in this version. I re-read the API for javax.sound.sampled and re-wrote the SoundClip class (formerly KitClip) from scratch. It think it’s cleaner and more usefull for other projects now.

I also added in a file chooser class, DrumChooser so there aren’t any absolute paths in the source anymore. I also added to that class a minor key-fetching function. I hope to make the kit’s expandable, as many drums as you can until you hit a LineUnavailableException.

The GUI hasn’t changed a lick, and I need to clean it up and develop a new layout for it and for the “multiple-drum” expandable version. It’s surprising what you can learn from an API.

DrumKit v0.01

September 30, 2006 » Geek

I finally started work on my DrumKit project today. Essentially this is an attempt to create a electronic drum set from some pads, an old keyboard and a computer. I got down to buisness and wrote up a Java app that plays back the drum sounds on key press events. It filters them and even handles shift for the hi-hat open/close, which I’ll use to make the hi-hat pedal work.

The source is messy, and still has relative path’s for the sound files. Essentially it’s a frame, a keylistener and some clips. Not too rough, though patching this together from the Java API and sparse information on javax.sound.sampled was tougher than I guessed it would be. Anyway, it’s got some bugs and features not implemented, but I can play drums with my keyboard now, which is a good start.

DrumKit v0.01
The Cutting Edge DrumKit GUI

I suppose I should upload the code just in case, and to keep track of my versions. You can get the source and the sounds for Version 0.01 below. As a heads up, all the kit samples are absolute path’d for my machine, so you’ll need to adjust them if you intend to compile it.
» DrumKit001.tar.gz – Everything
» DrumKit.java – Main class
» KeyHandler.java – Event listener
» KitClip.java – Kit sample class