Deserialize json character as enumeration

You don’t necessary need a custom JsonConverter you can use the built in StringEnumConverter with the combination of the EnumMemberAttribute (from the System.Runtime.Serialization assembly). Without the EnumMemberAttribute it uses the enum names so Artist, Contemporary, etc so you need to change the names with it to your A,C, etc value. But it is not the … Read more

How to add a method to Enumeration in Scala?

Here is an example of adding attributes to scala enums by extending the Enumeration.Val class. object Planet extends Enumeration { protected case class Val(val mass: Double, val radius: Double) extends super.Val { def surfaceGravity: Double = Planet.G * mass / (radius * radius) def surfaceWeight(otherMass: Double): Double = otherMass * surfaceGravity } implicit def valueToPlanetVal(x: … Read more

Enumerate over an enum in C++

To add to @StackedCrooked answer, you can overload operator++, operator– and operator* and have iterator like functionality. enum Color { Color_Begin, Color_Red = Color_Begin, Color_Orange, Color_Yellow, Color_Green, Color_Blue, Color_Indigo, Color_Violet, Color_End }; namespace std { template<> struct iterator_traits<Color> { typedef Color value_type; typedef int difference_type; typedef Color *pointer; typedef Color &reference; typedef std::bidirectional_iterator_tag iterator_category; }; … Read more

What is the best way to handle constants in Ruby when using Rails?

You can use an array or hash for this purpose (in your environment.rb): OPTIONS = [‘one’, ‘two’, ‘three’] OPTIONS = {:one => 1, :two => 2, :three => 3} or alternatively an enumeration class, which allows you to enumerate over your constants as well as the keys used to associate them: class Enumeration def Enumeration.add_item(key,value) … Read more

Which Typesafe Enum in C++ Are You Using?

I’m currently playing around with the Boost.Enum proposal from the Boost Vault (filename enum_rev4.6.zip). Although it was never officially submitted for inclusion into Boost, it’s useable as-is. (Documentation is lacking but is made up for by clear source code and good tests.) Boost.Enum lets you declare an enum like this: BOOST_ENUM_VALUES(Level, const char*, (Abort)(“unrecoverable problem”) … Read more