Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:47:34+00:00 2026-06-11T05:47:34+00:00

Short, at-a-glance summary of question (TLDR): how do I ensure a Java Object is

  • 0

Short, at-a-glance summary of question (TLDR): how do I ensure a Java Object is of type Map<String, Object>?


I have a YAML document as a String. I create the YAML using SnakeYAML from a Map<String, Object> but it could be tampered with before I read it back again. I don’t want to resort to PKI signing, it’s enough to see whether the YAML representation is still a Map<String, Object>, even if the Object values may be all over the place. I care about type, not content.

So: how do I ensure an Object is indeed of type Map<String, Object>?

Here’s how I am trying to circumvent the various SuppressWarnings of type erasure:

Object yamlObject = yaml.load(someYamlDocumentAsString);
// I only want to proceed if this YAML object is a Map<String, Object>
if ((yamlObject instanceof Map<?, ?>)) {
  Map<?, ?> propertyMap = (Map<?, ?>) yamlObject;
  for (Entry<?, ?> propertyEntry : propertyMap.entrySet()) {
    // I am checking each key and throwing if it's not a "String"
    Object propertyKey = propertyEntry.getKey();
    if (!(propertyKey instanceof CharSequence)) {
      throw new BananaException("FIXME"); // FIXME
    }
  }
} else {
  throw new CoconutException("FIXME"); // FIXME
}

Can the above be done better?

Incidentally, I don’t suppose Java has something akin to C#’s as keyword (from MSDN: The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception.)?


After the code above, can I safely suppress warnings for this:

@SuppressWarnings("unchecked")
Map<String, Object> aBeautifulAndCozyMap = (Map<String, Object>) propertyMap;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:47:35+00:00Added an answer on June 11, 2026 at 5:47 am

    Because of type erasure, information is not known at runtime, so there is no other way of checking it than instanceof, getClass().getName() or getDeclaredField("...").getGenericType().getActualTypeArguments() things.

    In my opinion you should stay with instanceof as it is easily readable and other developer will easily understand what you wanted to do.

    PS: I liked the exceptions 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short question you guys: I have a PHP string which may or maybe not
Short Question: Given Clojure's concurrency model, how do I ensure that all LWJGL OpenGL
Short Question: If I have a Silverlight application built in SL3 can I detect
Short question: Is it possible to have N work processes and a balancer process
Short question, is the following ok: struct X { A& x; A y; X()
Short and simple question. In Winforms you can bind a dataview to a combobox
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate
Short Question: Can I specify wildcards for custom console folding? If so, what is
I'd like to have an app that offered an at a glance view of
Short Question If my application is already rendering at 60fps, is there any additional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.